selenium automation, sometimes encounter situations need to simulate mouse actions to carry out, such as click, double click, right click, drag, and so on. And selenium provides us with a class to handle such events --ActionChains.
The main operational processes:
1. Storage mouse to operate;
2.perform () to perform mouse operations.
Examples are as follows:
Import ActionChains from selenium.webdriver.common.action_chains #. 1. Examples of mouse-type AC = ActionChains (Driver) # 2. After calling various mouse behavior, calling perform () performs ac.move_to_element_with_offset (driver.find_element_by_xpath ( '// div [@ id = "u" ] / * [@ name = "tj_login"] ')). perform ()
ActionChains mouse class of common methods :
- context_click () Right-click
- double_click () Double-click on
- drag_and_drop () drag
- move_to_element () a mouse over the element
- click_and_hold () pressing the left mouse button on an element
Before the following method ActionChains class, we need the introduction of class ActionChains
from selenium.webdriver.common.action_chains import ActionChains
AC. A method name () method name ...... n (). Perform ()
Here should be noted that: ActionChains (Driver),
Driver: Examples of the webdriver user operation is performed.
ActionChains for producing a user's behavior, the behavior of all the objects stored on actionchains, then the execution behavior of all stored by ActionChains perform ().
Method perform () is also provided ActionChains class, generally ActionChains () with the use.
Right-click context_click () operation
# Targeting element to right click right driver.find_element_by_xpath = ( "XX") # of positioning elements to perform operations Right ActionChains (driver) .context_click (right) .perform ()
Double click DOUBLE_CLICK () operation
To double-click # targeting element Double driver.find_element_by_xpath = ( "XXX") # of positioning elements to perform mouse double click operation ActionChains (driver) .double_click (double) .perform ()
Drag and drop drag_and_drop () operation
Positioning the home position of the element # Element driver.find_element_by_name = ( "XXX") # positioning element to move to the target position of the target driver.find_element_by_name = ( "XXX") # moving element operation performed ActionChains (driver) .drag_and_drop (element, target) .perform ()
Move_to_element mouse suspended on an element ()
# Mouse requires suspended positioning element ELE = driver.find_element_by_id ( 'I1') # perform mouse operations ActionChains (driver) .move_to_element (ele) .perform ()