Selenium+python Automation 93-mouse events (Actionchains) source detailed

Source: Internet
Author: User

Actionchains Introduction

Actionchains is a selenium that specializes in mouse-related operations such as mouse movement, mouse button manipulation, keystrokes and context menus (right mouse buttons) interaction.
This is useful for making more complex moves, such as hover and drag-and-drop.

Actionchains can also be used with shortcut keys, such as Ctrl,shif,alt combined with the mouse.

When you use the Actionchains object method, the behavior event is stored in the Actionchains object queue. When you use Perform (), the events are executed sequentially.

    • Method One: Can write a long list
menu = driver.find_element_by_css_selector(".nav")hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
    • Method Two: Can be divided into several steps to write
menu = driver.find_element_by_css_selector(".nav")hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")actions = ActionChains(driver)actions.move_to_element(menu)actions.click(hidden_submenu)actions.perform()

Either way, the operations are executed in the order in which they are called!

Method Introduction

1. The keys class mentioned below is the keyboard event class inside selenium, and the method of importing:

From Selenium.webdriver.common.keys import keys

2. Use the import method for mouse events:

From Selenium.webdriver.common.action_chains import Actionchains

Class Actionchains (object): Def __init__ (self, driver): Self._driver = Driver Self._actions = [] def p Erform (self): # Execute Behavior Event def click (Self, On_element=none): Click:-If the parameter is not written, then the current mouse position is clicked-if the parameter is written        Bit to the element object elements, that is the point of the element def click_and_hold (self, on_element=none): left mouse button to hold an element-if the argument is not written, then the point is the current mouse position        -If the parameter is written anchored to the element object elements, that is the point of the element def Context_click (self, on_element=none): right mouse click-if the parameter is not written, then the point is the current mouse position        -If the parameter is written to the element object elements, that is the element def double_click (self, On_element=none): Double-click the mouse-if the parameter is not written, then the point is the current mouse position        -If the parameter is written to the element object elements, that is the dot element def drag_and_drop (self, source, target): Press and hold the left mouse button on the source element, then move to the target element and release the mouse buttons -Source: Hold down the mouse element position-target: Release the mouse element position def drag_and_drop_by_offset (self, source, Xoffset, Yoffset): Press        The left mouse button on the source element, and then moves to the target offset and releases the mouse buttons. -Source: Position of the element holding the mouse-xoffset:x axis offset-yoffset:y axis offset def key_down (self, value, element=None): Sends only one key without releasing it.        Applies only to modifier keys (control, ALT, and shift). -Value: The modifier keys to send.        Values are defined in the Keys class. -element: Positioned elements If the parameter is not written is the current position of the mouse for example, press CTRL + C: Actionchains (Driver). Key_down (Keys.control). Send_keys (' C '). KEY_UP (Keys.control). Perform () def key_up (self, Value, Element=none): # Release the keys and use DEF mov together with the above E_by_offset (self, Xoffset, yoffset): The offset to move the mouse to the current mouse position-the xoffset:x axis moves to the X offset as a positive or negative integer-yoffset:y    The axis offset, as a positive or negative integer.  def move_to_element (self, to_element): hover-to_element: Locates the element that needs to be hovered def move_to_element_with_offset (self, To_element, Xoffset, Yoffset): Moves the mouse by specifying the offset of the element. Offset relative to the upper-left corner of the element-to_element: Locating the element that needs to be hovered-xoffset:x axis offset-yoffset:y axis offset def release (self, On_elem        Ent=none): Releases the mouse button on an element.    -If the parameter is not written, then is the current mouse position-if the parameter is written to the element object, it is this element. def send_keys (self, *keys_to_send): The key to send to the current focus element.       Modifier-Keys constants can be used in the button class. def send_keys_to_element (self, element, *keys_to_send): Sent to the anchored element-element: Positioned elements-keys_to_send: The key to be sent. Modifier-Keys constants can be used in the button class.

Source code can be viewed in the following directory: lib\site-packages\selenium\webdriver\common\action_chains.py

Selenium+python Automation 93-mouse events (Actionchains) source detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.