Selenium-ActionChains Api接口详解
ActionChains
有时候我们在通过Selenium做UI自动化的时候,明明能够在DOM树内看到这个元素,但是我在通过driver click、sendkey的时候,就是点击不到或无法输入字符串。实际上这是由于WEB中某些元素需要通过一系列连贯的操作才能处于可以点击的状态,driver提供的click方法是每次都只执行一个命令操作,而我们需要连贯的操作。或者经常遇到那种,需要鼠标悬浮后,要操作的元素才会出现的某种场景,那么我们就要模拟鼠标悬浮到某一个位置,做一系列的连贯操作,这里就要应用Selenium提供的ActionChains模块
具体使用方法参考官方文档: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains
或某大佬翻译的中文文档:https://python-selenium-zh.readthedocs.io/zh_CN/latest/7.2%20%E8%A1%8C%E4%B8%BA%E9%93%BE/
引入方式
#引入方式一 from selenium.webdriver.common.action_chains import ActionChains #引入方式二 from selenium.webdriver import ActionChains
实际上ActionChains这个模块的实现的核心思想就是,当你调用ActionChains的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个List里,当你调用perform()方法时,队列中的时间会依次执行。(注:推荐一个尺子工具,MeasulerIt)
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) ——发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素
drag_and_drop(鼠标拖动)
# 将source元素拖放至target元素处,参数为两个elementObj
ActionChains(driver).drag_and_drop(source=source,target=target)
# 将一个source元素 拖动到针对source坐上角坐在的x y处 可存在负宽度的情况和负高度的情况
ActionChains(driver).drag_and_drop_by_offset(source, x, y)
# 这种也是拖拽的一种方式,都是以源元素的左上角为基准,移动坐标
ActionChains(driver).click_and_hold(dom).move_by_offset(169,188).release().perform()
move_to_element
# 鼠标移动到某一个元素上,结束elementObj
ActionChains(driver).move_to_element(e)
# 鼠标移动到制定的坐标上,参数接受x,y
ActionChains(driver).move_by_offset(e['x'],e['y'])
例:
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.baidu.com')
time.sleep(2)
# driver.execute_script('document.body.scrollTop=0')
driver.execute_script('document.documentElement.scrollTop')
time.sleep(1)
a = driver.find_element_by_id('a').location
dis = driver.find_element_by_id('dis1')
ActionChains(driver).move_by_offset(a['x'],a['y']).double_click(dis).perform()
click
# 单击事件,可接受elementObj
ActionChains(driver).click()
# 双击事件,可接受elementObj
ActionChains(driver).double_click()
# 点击鼠标右键
ActionChains(driver).context_click()
# 点击某个元素不松开,接收elementObj
ActionChains(driver).click_and_hold()
# # 某个元素上松开鼠标左键,接收elementObj
ActionChains(driver).release()
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_actionchains.py @time:2020/09/27 """ import pytest from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains class TestDefaultSuite(): def setup_method(self, ): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(5) def teardown_method(self): self.driver.quit() # 鼠标点击测试 # @pytest.mark.skip def test_case_click(self): self.driver.get("http://sahitest.com/demo/clicks.htm") click_btn = self.driver.find_element_by_xpath('//input[@value="click me"]') # 单击按钮 doubleclick_btn = self.driver.find_element_by_xpath('//input[@value="dbl click me"]') # 双击按钮 rightclick_btn = self.driver.find_element_by_xpath('//input[@value="right click me"]') # 右键单击按钮 # action=ActionChains(self.driver) # action.click(click_btn) # 单击 # action.double_click(doubleclick_btn) # 双击 # action.context_click(rightclick_btn) # 右键 # action.perform() # sleep(5) # 或者直接以长链的方式执行,按照顺序依次执行 ActionChains(self.driver).click(click_btn).double_click(doubleclick_btn).context_click(rightclick_btn).perform() # 鼠标移动测试 def test_case_move(self): # self.driver.get('https://www.baidu.com/') # set_ele=self.driver.find_element_by_id('s-usersetting-top') # 百度的设置元素 # ActionChains(self.driver).move_to_element(set_ele).perform() # sleep(5) self.driver.get('http://sahitest.com/demo/mouseover.htm') write = self.driver.find_element_by_xpath( '//input[@value="Write on hover"]') # 鼠标移动到此元素,在下面的input框中会显示“Mouse moved” blank = self.driver.find_element_by_xpath('//input[@value="Blank on hover"]') # 鼠标移动到此元素,会清空下面input框中的内容 result = self.driver.find_element_by_name('t1') action = ActionChains(self.driver) action.move_to_element(write).perform() # 移动到write,显示“Mouse moved” print(result.get_attribute('value')) # action.move_to_element(blank).perform() action.move_by_offset(10, 50).perform() # 移动到距离当前位置(10,50)的点,与上句效果相同,移动到blank上,清空 print(result.get_attribute('value')) action.move_to_element_with_offset(blank, 10, -40).perform() # 移动到距离blank元素(10,-40)的点,可移动到write上 print(result.get_attribute('value')) sleep(2) # 鼠标拖拽 def test_case_drapdrop(self): self.driver.get('http://sahitest.com/demo/dragDropMooTools.htm') dragger = self.driver.find_element_by_id('dragger') # 被拖拽元素 item1 = self.driver.find_element_by_xpath('//div[text()="Item 1"]') # 目标元素1 item2 = self.driver.find_element_by_xpath('//div[text()="Item 2"]') # 目标2 item3 = self.driver.find_element_by_xpath('//div[text()="Item 3"]') # 目标3 item4 = self.driver.find_element_by_xpath('//div[text()="Item 4"]') # 目标4 action = ActionChains(self.driver) action.drag_and_drop(dragger, item1).perform() # 1.移动dragger到目标1 sleep(2) action.click_and_hold(dragger).release(item2).perform() # 2.效果与上句相同,也能起到移动效果 sleep(2) action.click_and_hold(dragger).move_to_element(item3).release().perform() # 3.效果与上两句相同,也能起到移动的效果 sleep(2) # action.drag_and_drop_by_offset(dragger, 400, 150).perform() # 4.移动到指定坐标 action.click_and_hold(dragger).move_by_offset(400, 150).release().perform() # 5.与上一句相同,移动到指定坐标 sleep(2) if __name__ == '__main__': pytest.main()
执行结果
Testing started at 12:59 上午 ... /usr/local/bin/python3.9 "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_selenium/test_actionchains.py Launching pytest with arguments /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_selenium/test_actionchains.py in /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_selenium ============================= test session starts ============================== platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.9 cachedir: .pytest_cache rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_selenium plugins: allure-pytest-2.8.18 collecting ... collected 3 items test_actionchains.py::TestDefaultSuite::test_case_click test_actionchains.py::TestDefaultSuite::test_case_move test_actionchains.py::TestDefaultSuite::test_case_drapdrop ============================== 3 passed in 37.93s ============================== Process finished with exit code 0 PASSED [ 33%]PASSED [ 66%]Mouse moved Mouse moved PASSED [100%]
有时我们需要模拟键盘操作时,那么就需要用到ActionChains中的key操作了,提供了两个方法,key_down与key_up,模拟按下键盘的某个键子,与松开某个键子,接收的参数是按键的Keys与elementObj。可以与send_keys连用(例:全选、复制、剪切、粘贴)
# key_down 模拟键盘摁下某个按键 key_up 松开某个按键,与sendkey连用完成一些操作,每次down必须up一次否则将出现异常
ActionChains(driver).key_down(Keys.CONTROL,dom).send_keys('a').send_keys('c').key_up(Keys.CONTROL)\
.key_down(Keys.CONTROL,dom1).send_keys('v').key_up(Keys.CONTROL).perform()
Keys 实际是Selenium提供的一个键盘事件模块,在模拟键盘事件时需要导入Keys模块
引入路径
from selenium.webdriver.common.keys import Keys
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_actionchains.py @time:2020/09/27 """ import pytest from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys class TestDefaultSuite(): def setup_method(self, ): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(5) def teardown_method(self): self.driver.quit() def test_key(self): self.driver.get('https://www.baidu.com/') dom = self.driver.find_element_by_id('kw') ActionChains(self.driver).key_down(Keys.SHIFT, dom).send_keys('a').send_keys('c').key_up(Keys.SHIFT).pause(2).perform()
end