ActionChains类鼠标操作的常用方法
Posted AVICII_2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionChains类鼠标操作的常用方法相关的知识,希望对你有一定的参考价值。
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 中存储的行为
以上是关于ActionChains类鼠标操作的常用方法的主要内容,如果未能解决你的问题,请参考以下文章