介绍
- 官方文档:https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains
- ActionChains:执行PC端的鼠标点击、双击、右键、拖拽等事件
- TouchActions:模拟PC和移动端的点击、滑动、拖拽、多点触控等多种手势操作
执行原理
- 调用ActionChains的方法时,不会立即执行,而是将所有的操作,按顺序存放在一个队列里,当你调用perform()方法时,队列中的事件会依次执行
基本用法
- 生成一个动作action=ActionChains(driver)
- 动作添加方法1 action.方法1
- 动作添加方法2 action.方法2
- 调用perform()方法执行(action.perform())
写法
鼠标移动到某个元素上
- action=ActionChains(self.driver)
- action.move_to_element(element)
- action.perform()
将一个元素拖拽到某个元素上
- drag_element
- drop_element
- action=ActionChains(self.driver)
- action.drag_and_drop(drag_element, drop_element) 写法一
- action.click_and_hold(drag_element).release(drop_element) 写法二
- action.click_and_hold(drag_element).move_to_element(drop_element) 写法三
- action.perform()
键盘操作
- 输入内容:action.send_keys(“hello”)
- 按下空格:action.send_keys(Keys.SPACE)
- 按下删除键:action.send_keys(Keys.BACK_SPACE)
- 快捷键复制: action.key_down(Keys.COMMAND).send_keys(“a”).key_up(Keys.COMMAND)
import time
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()
self.driver.implicitly_wait(5)
self.driver.maximize_window()
def teardown(self):
self.driver.quit()
def test_click(self):
self.driver.get("http://sahitest.com/demo/clicks.htm")
element_click = self.driver.find_element(By.CSS_SELECTOR, 'input[value="click me"]')
element_double_click = self.driver.find_element(By.CSS_SELECTOR, 'input[value="dbl click me"]')
element_right_click = self.driver.find_element(By.CSS_SELECTOR, 'input[value="right click me"]')
action = ActionChains(self.driver)
action.click(element_click).double_click(element_double_click).context_click(element_right_click).perform()
time.sleep(3)
def test_move_to_element(self):
self.driver.get("https://www.baidu.com/")
ele = self.driver.find_element_by_css_selector("#s-usersetting-top")
action = ActionChains(self.driver)
action.move_to_element(ele)
action.perform()
time.sleep(3)
def test_drag_drop(self):
self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
dragger_element = self.driver.find_element_by_id('dragger')
drop_element = self.driver.find_element_by_xpath("//body/div[2]")
action = ActionChains(self.driver)
action.click_and_hold(dragger_element).move_to_element(drop_element)
action.perform()
time.sleep(3)
def test_key(self):
self.driver.get("http://sahitest.com/demo/label.htm")
user_name_element = self.driver.find_element_by_xpath("/html/body/label[1]/input")
user_name_element.click()
action = ActionChains(self.driver)
action.send_keys("hello")
action.pause(3)
action.send_keys(Keys.SPACE)
action.pause(3)
action.send_keys("world!")
action.pause(3)
action.send_keys(Keys.BACK_SPACE)
action.pause(3)
action.key_down(Keys.COMMAND).send_keys("a").key_up(Keys.COMMAND)
action.pause(3)
action.perform()
time.sleep(3)
document.querySelectorAll(’*’)
可以使用写法 $(’*’) 是因为页面使用了 JQuery 这个库
css表达式写法一致