官方文档:https://selenium-python.readthedocs.io/api.html
ActionChains
执行PC端的鼠标右键、单击、双击、拖拽等事件
调用ActionChains的方法时,不会立即执行,而是将所有的操作,按顺序存放到一个队列中,当调用perform()方法时,队列中的事件会依次执行
链式写法:
ActionChains(driver).move_to_element(element).click(element).perform()
分步写法:
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()
示例
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class TestActionChains:
def setup(self):
self.driver = webdriver.Chrome(executable_path='./chromedriver.exe')
def teardown(self):
self.driver.quit()
# 单击、双击、右键
def test_click(self):
self.driver.get('http://sahitest.com/demo/clicks.htm')
self.driver.maximize_window()
self.driver.implicitly_wait(3)
click_me = self.driver.find_element(By.CSS_SELECTOR, '[value="click me"]')
dbl_click_me = self.driver.find_element(By.CSS_SELECTOR, '[value="dbl click me"]')
right_click_me = self.driver.find_element(By.CSS_SELECTOR, '[value="right click me"]')
action = ActionChains(self.driver)
action.click(click_me)
action.double_click(dbl_click_me)
action.context_click(right_click_me)
action.perform()
sleep(3)
# 鼠标移动到某个元素上
def test_move_element(self):
self.driver.get("http://wwww.baidu.com")
self.driver.maximize_window()
self.driver.implicitly_wait(3)
setting = self.driver.find_element(By.CSS_SELECTOR, '#s-usersetting-top')
action = ActionChains(self.driver)
action.move_to_element(setting)
action.perform()
sleep(3)
# 拖拽
def test_drag_drop(self):
self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
self.driver.maximize_window()
self.driver.implicitly_wait(3)
dragger = self.driver.find_element(By.XPATH, '//*[@id="dragger"]')
item1 = self.driver.find_element(By.XPATH, '/html/body/div[2]')
item2 = self.driver.find_element(By.XPATH, '/html/body/div[3]')
item3 = self.driver.find_element(By.XPATH, '/html/body/div[4]')
action = ActionChains(self.driver)
# action.drag_and_drop(dragger, item1).perform()
# action.click_and_hold(dragger).release(item2).perform()
action.click_and_hold(dragger).move_to_element(item3).release().perform()
sleep(3)
# 模拟按键
def test_key(self):
self.driver.get("http://sahitest.com/demo/label.htm")
username = self.driver.find_element_by_xpath("/html/body/label[1]/input")
username.click()
action = ActionChains(self.driver)
action.send_keys("xiaoming").pause(1)
action.send_keys(Keys.SPACE).pause(1)
action.send_keys("xiaohong").pause(1)
action.send_keys(Keys.BACK_SPACE).pause(1)
action.perform()
sleep(3)
# 复制粘贴
def test_copy_paste(self):
self.driver.get("http://sahitest.com/demo/label.htm")
username1 = self.driver.find_element_by_xpath("/html/body/label[1]/input")
username2 = self.driver.find_element_by_xpath("/html/body/label[2]/table/tbody/tr/td[2]/input")
username1.send_keys("xiaoming")
username1.send_keys(Keys.CONTROL, 'a')
sleep(1)
username1.send_keys(Keys.CONTROL, 'c')
sleep(1)
username2.send_keys(Keys.CONTROL, 'v')
sleep(3)