一.Actions
ActionChains:执行PC端鼠标的点击,双击,右键,拖拽等事件
TouchActions:模拟PC端和移动端(h5页面)的点击,滑动,拖拽,多点触控等多种手势
二.动作链接ActionChains
参考:7.2 行为链 - selenium-python中文文档 (python-selenium-zh.readthedocs.io)
1.原理:
调用ActionChains的方法时,不会立即执行,而是将所有的操作,按顺序放在一个队列中,当你调用perform()方法时,队列中事件会依次执行
2.基本用法:
- 生成一个动作action = ActionChains(driver)
- 动作添加方法1:action.方法1
调用perform()方法执行action.perform()
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from time import sleep
class TestAction:
def setup(self):
self.driver = webdriver.Chrome()
# 隐式等待
self.driver.implicitly_wait(5)
self.driver.get("https://www.baidu.com/")
self.driver.maximize_window()
def teardown(self):
self.driver.quit()
def test_baidu(self):
self.driver.find_element(By.ID, "kw").send_keys("格沃兹测试学院")
self.driver.find_element(By.ID, "su").click()
el = self.driver.find_element_by_link_text("设置")
action = ActionChains(self.driver)
action.move_to_element(el)
action.perform()
sleep(3)
3.ActionChains方法列表 click(on_element=None) ——单击鼠标左键 click_and_hold(on_element=None) ——点击鼠标左键,不松开 context_click(on_element=None) ——点击鼠标右键 double_click(on_element=None) ——双击鼠标左键 drag_and_drop(source, target) ——拖拽到某个元素然后松开 drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开 key_down(value, element=None) ——按下某个键盘上的键 key_up(value, element=None) ——松开某个键 move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标 move_to_element(to_element) ——鼠标移动到某个元素 move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置 perform() ——执行链中的所有动作 release(on_element=None) ——在某个元素位置松开鼠标左键 send_keys(*keys_to_send) ——发送某个键到当前焦点的元素 eg:action.send_keys(Key.SPACE) 输入空格,基本上键盘上的操作都可以模拟 send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素
三、TouchActions
参考:Selenium API PY 文档 - selenium.webdriver.common.touch_actions (liushilive.github.io)
主要方法有:
- double_tap(on_element) 双击
- flick_element(on_element, xoffset, yoffset, speed) 从元素开始以指定的速度移动
- long_press(on_element) 长按不释放
- move(xcoord, ycoord) 移动到指定的位置
- perform() 执行链中的所有动作
- release(xcoord, ycoord) 在某个位置松开操作
- scroll(xoffset, yoffset) #滚动到某个位置
- scroll_from_element(on_element, xoffset, yoffset) 从某元素开始滚动到某个位置
- tap(on_element) 单击
- tap_and_hold(xcoord, ycoord) 某点按
from selenium import webdriver from selenium.webdriver import ActionChains, TouchActions from selenium.webdriver.common.by import By from time import sleep class TestCeshiren: def setup(self): opt = webdriver.ChromeOptions() opt.add_experimental_option('w3c', False) self.driver = webdriver.Chrome(chrome_options=opt) # 隐式等待 self.driver.implicitly_wait(5) self.driver.get("https://www.baidu.com") self.driver.maximize_window() def teardown(self): self.driver.quit() def test_TouchActions(self): el = self.driver.find_element(By.ID, "kw") el_search = self.driver.find_element(By.ID, "su") el.send_keys("selenium测试") action = TouchActions(self.driver) action.tap(el_search) action.perform() action.scroll_from_element(el, 0, 10000).perform() sleep(3)
备注:执行报错:
self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x000002537D0AF7F0>
response = {‘status’: 404, ‘value’: ‘{“value”:{“error”:“unknown command”,“message”:“unknown command: Cannot call non W3C standard…jectPath [0x778387A4+228]\n\tRtlGetAppContainerNamedObjectPath [0x77838774+180]\n\t(No symbol) [0x00000000]\n”}}’}
解决:
设置下这个参数
opt = webdriver.ChromeOptions()
opt.add_experimental_option('w3c', False)
driver = webdriver.Chrome(chrome_options=opt)
因为当前响应不是标准的W3C规范,所以无法处理,关闭W3C验证就可以了