Selenium+python Automation 94-Behavioral events (actionchains) Source Details

Source: Internet
Author: User
Tags modifier

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

ClassActionchains(object):Def__init__(Self, driver): Self._driver = Driver Self._actions = []DefPerform(self):# Execution Behavior EventsDefClick(Self, On_element=none): Click:-If the parameter is not written, then click on the current mouse position-if the parameter is positioned to the element object elements, that is the point of this elementDefClick_and_hold(Self, On_element=none): The left mouse button holds down an element-if the argument is not written, then the point is the current mouse position-if the parameter is written to the element object elements, that is the point elementDefContext_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 to navigate to the element object elements, that is the point of this elementDefDouble_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 to navigate to the element object elements, that is the point of this elementDefDrag_and_drop(self, source, target): Press and hold the left mouse button on the source element, then move to the target element and release mouse buttons-source: Hold down the mouse element position-target: Release the mouse element positionDefDrag_and_drop_by_offset(self, source, Xoffset, Yoffset): Press and hold the left mouse button on the source element, and then move to the target offset and release the mouse buttons. -Source: Position of the element holding down the mouse-offset of the xoffset:x axis-yoffset:y axisDefKey_down(Self, value, Element=none): Sends only one key and does not release 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 ()DefKey_up(Self, value, Element=none):# Release the keys to use with the aboveDefMove_by_offset(Self, Xoffset, yoffset): The offset that moves the mouse to the current mouse position-the xoffset:x axis moves to the X offset-yoffset:y axis offset as a positive or negative integer as a positive or negative integer.DefMove_to_element(Self, to_element): hover-to_element: Locate the element you want to hover overdef move_to_element_with_offset def releasedef send_keys def send_keys_to_element
To cite a case

1. Implementing the CTRL +f5 key combination function

# Coding:utf-8From seleniumimport webdriverfrom selenium.webdriver.common.action_chains import actionchainsfrom Selenium.webdriver.common.keys import keysimport timedriver = Webdriver. firefox () driver.get ( "https:// Www.baidu.com ") time.sleep (3) # Implement CTRL +f5 refresh actionchains (Driver). Key_down (keys. CONTROL). Send_keys (keys. F5). KEY_UP (keys. CONTROL). Perform ()              

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

Selenium+python Automation 94-Behavioral events (actionchains) Source Details

Related Article

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.