We are doing automated time, sometimes use some mouse operations, such as right-click, double click and other operations, selenium ActionChains to provide for some of our mouse-related operations
First, we need to import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
Short answer, for example, ActionChains supports the following operations:
1. Click operation: click ()
2. Right-click: context_click ()
3. Double-click operation: double_click ()
4. Hover: move_to_element ()
Of course, there are a lot of mouse actions, I will not list them here
Note: After all the operations we need to preform () to perform actions
The following column two simple examples:
Hover operation:
from Selenium Import the webdriver from selenium.webdriver.common.action_chains Import ActionChains Import Time '' ' 1. Examples of mouse objects 2 to be operated to find the page elements 3. mouse ' '' Driver = webdriver.Chrome () Driver. GET ( " https://www.baidu.com/ " ) the time.sleep ( 2 ) # instantiated objects mouse Action = ActionChains (Driver) # positioning element [Setup] setting = driver.find_element_by_xpath ( " // div [@ id = 'u1'] // a [text () = ' settings'] ") #Mouse to move to the element, mouseover action.move_to_element (setting) .perform ()
Right-click action:
from Selenium Import the webdriver from selenium.webdriver.common.action_chains Import ActionChains Import Time '' ' 1. Examples of mouse objects 2 to be operated to find the page elements 3. mouse ' '' Driver = webdriver.Chrome () Driver. GET ( " https://www.baidu.com/ " ) the time.sleep ( 2 ) # instantiate an object mouse Action = ActionChains (Driver) # positioning elements [set] /// I have here is random orientation, we can the positioning element itself to be operated setting = driver.find_element_by_xpath ( " // div [@ ID =" U1 '] // a [text () =' settings'] ") # Right-click action.context_click (setting) .perform ()