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 中存储的行为

posted @ 2019-05-05 23:54  Avicii_2018  阅读(1699)  评论(0编辑  收藏  举报