Python 鼠标操作|ActionChains类常用方法

发表时间:2020-01-03

ActionChains是自动执行低级交互的一种方式,例如:鼠标移动,鼠标点按,键盘操作,文本操作等。

当我们调用这里的方法时,这些操作会被先储存在一个队列中,当我们调用perform()方法时,队列中的操作会被按顺序执行,执行后队列被清空。


常用方法:

1.context_click() 鼠标右击

2.double_click() 鼠标双击

3.drag_and_drop() 鼠标拖动

4.move_to_element() 鼠标悬停在一个元素上

5.click_and_hold() 按下鼠标左键在一个元素上,release()释放

1.context_click()鼠标右击

1 #-*-coding:utf-8-*-
2 from selenium import webdriver
3 from selenium.webdriver.common.action_chains import ActionChains
4 driver = webdriver.Chrome()
5 driver.maximize_window()
6 driver.get("http://www.baidu.com")
7 element = driver.find_element_by_link_text('hao123')
8 ActionChains(driver).context_click(element).perform()

2.double_click()鼠标双击

1 #-*-coding:utf-8-*-
2 from selenium import webdriver
3 from selenium.webdriver.common.action_chains import ActionChains
4 driver = webdriver.Chrome()
5 driver.maximize_window()
6 driver.get("http://www.baidu.com")
7 element = driver.find_element_by_link_text('hao123')
8 ActionChains(driver).double_click(element).perform()

3.drag_and_drop() 鼠标拖动

1 #-*-coding:utf-8-*-
2 from selenium import webdriver
3 from selenium.webdriver.common.action_chains import ActionChains
4 driver = webdriver.Chrome()
5 driver.maximize_window()
6 driver.get("http://www.baidu.com")
7 source = driver.find_element_by_link_text('新闻')
8 target = driver.find_element_by_id('kw')
9 ActionChains(driver).drag_and_drop(source,target).perform()

4.move_element_to()

 1 #-*-coding:utf-8-*-
 2 from selenium import webdriver
 3 from selenium.webdriver.common.action_chains import ActionChains
 4 import time
 5 driver = webdriver.Chrome()
 6 driver.maximize_window()
 7 driver.get("http://www.baidu.com")
 8 more_products = driver.find_element_by_link_text('更多产品')
 9 ActionChains(driver).move_to_element(more_products).perform()
10 driver.find_element_by_link_text('音乐').click()
11 time.sleep(3)
12 driver.quit()

5.click_and_hold()按下鼠标左键在一个元素上,release()释放

 1 #-*-coding:utf-8-*-
 2 from selenium import webdriver
 3 from selenium.webdriver.common.action_chains import ActionChains
 4 import time
 5 driver = webdriver.Chrome()
 6 driver.maximize_window()
 7 driver.get("http://www.baidu.com")
 8 element = driver.find_element_by_link_text('新闻')
 9 ActionChains(driver).click_and_hold(element).perform()
10 ActionChains(driver).release(element).perform()
11 time.sleep(3)
12 driver.quit()

 注:perform()执行所有 ActionChains 中存储的行为

文章来源互联网,尊重作者原创,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890


Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free