1. 程式人生 > >ActionChains類鼠標操作的常用方法

ActionChains類鼠標操作的常用方法

sel odi click mil max 鼠標懸停 baidu col chrome

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類鼠標操作的常用方法