Python+selenium notes (14) Mouse and keyboard events

Source: Internet
Author: User

(i) Preface

Webdriver Advanced Application API allows us to simulate simple to complex keyboard and mouse events, such as drag-and-drop operations, shortcut key combinations, long presses, and right-click Actions, all by using the Actionchains class in the Webdriver Python API.

called The Actionchains class method is not executed immediately, but all operations are stored in a queue, and when the Perform () method is called, the operations in the queue execute sequentially

(ii) some important methods related to keyboard and mouse events

Method

Simple description

Click (on_element=none)

Click Element

On_element: Refers to the element being clicked, if the parameter is none, the current mouse position is clicked

Click_and_hold (On_element=none)

Hold down the left mouse button on the element

On_element: Refers to the element to hold down the left mouse button, and if the parameter is none, the current mouse position is clicked.

Double_click (On_element=none)

Double-click an element

On_element: Whenever you double-click an element, if the parameter is none, click the current mouse position

Drag_and_drop (source, target)

Mouse drag

Source: The element that the mouse drags

Target: Mouse-released destination element

Key_down (value, Element=none)

Press and hold a key without releasing it for modifier keys (CTRL, ALT, and shift)

Vakue: Refers to the key to hold, the value is defined in the Keys class

element: The target element that is triggered by the key and, if none, is triggered at the current focus position.

Key_up (value, Element=none)

Release modifier keys

Vakue: Refers to the key to hold, the value is defined in the Keys class

element: The target element that is triggered by the key and, if none, is triggered at the current focus position.

Move_to_element (to_element)

Moves the mouse to the center of the specified element

To_element: Specifying elements

Perform ()

Commit a saved action

Release (On_element=none)

Release mouse

On_element: The element that is released by the mouse

Send_keys (Keys_to_send)

Keyboard action on the current focus element

Keys_to_send: Input value of keyboard

Send_keys_to_element (element, Keys_to_send)

Keyboard action on the specified element

Element: Specifying elements

Keys_to_send: Input value of keyboard


(c) example (keyboard event)

1  fromSelenium.webdriver.common.action_chainsImportActionchains2  fromSeleniumImportWebdriver3  fromSelenium.webdriver.common.keysImportKeys4 5Driver =Webdriver. Chrome ()6Driver.implicitly_wait (20)7 Driver.maximize_window ()8Driver.get ('https://www.cnblogs.com/')9 #Look for the search box .TenSearch_file = Driver.find_element_by_css_selector ('#zzk_q') One #Google search box ASearch_file_gg = Driver.find_element_by_css_selector ('#google_search_q') -  -Search_file.send_keys ('Test') the #Ctrl + A -Actionchains (Driver). Key_down (Keys.control). Send_keys ('a'). KEY_UP (Keys.control). Perform () - #Ctrl + C -Actionchains (Driver). Key_down (Keys.control). Send_keys ('C'). KEY_UP (Keys.control). Perform () + ##ctrl +v -Actionchains (Driver). Key_down (Keys.control). Send_keys_to_element (Search_file_gg,'v'). Perform () + #Ctrl + A AActionchains (Driver). Key_down (Keys.control). Send_keys ('a'). KEY_UP (Keys.control). Perform () at #ctrl+x -Actionchains (Driver). Key_down (Keys.control). Send_keys ('x'). KEY_UP (Keys.control). Perform () - #Ctrl + V -Actionchains (Driver). Key_down (Keys.control). Send_keys_to_element (Search_file,'v'). KEY_UP (Keys.control). Perform () - #Enter - actionchains (Driver). Send_keys (Keys.enter). Perform () in  -Driver.quit ()

(iv) example (mouse event)

1  fromSelenium.webdriver.common.action_chainsImportActionchains2  fromSeleniumImportWebdriver3 4Driver =Webdriver. Chrome ()5Driver.implicitly_wait (20)6 Driver.maximize_window ()7Driver.get ('https://www.cnblogs.com/')8 #Programming Languages9Program_lan = Driver.find_element_by_xpath ('//li[@id = "Cate_item_2"]/a')Ten #small class Python under programming language OneProgram_py = Driver.find_element_by_xpath ('//li/a[@href = "/cate/python/"]') A #Mouse first move to "programming language", then click Python - actionchains (Driver). Move_to_element (Program_lan). Click (program_py). Perform () -  theDriver.quit ()

(v) View the documentation for the Python selenium API

Click this below (or you can start python-m pydoc-p 3333 by command,-P refers to the port number (custom one)), and then after the browser login (http://localhost:3333/), enter selenium to query

Python+selenium notes (14) Mouse and keyboard events

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.