In the previous examples, the click events are realized by click() method for mouse click events. In fact, in the WebDriver, provides a number of mouse operation methods, these operation methods are encapsulated in the ActionChains class, including the mouse right-click, double-click, hover and drag the mouse and other functions.
Mouse action events provided by ActionChains class
- context_click(): Click the right mouse button.
- double_click():double click mouse
- drag_and_drop(): drag the mouse.
- move_to_element(): mouse hovering.
mouse hover
There are some drop-down menus on the page where the menu below is displayed after the mouse is placed over the element. The following image is an example
Baidu home page on the settings of the item, the following menu will not be displayed by default, the mouse will be parked on the top of the display will be displayed, this is the mouse hovering operation, the menu is displayed after the following items can be clicked on the action!
Code to access search settings
# -*- coding: utf-8 -*- from selenium import webdriver import time driver = () ("") (3) settings = driver.find_element_by_link_text('Settings') (driver).move_to_element(settings).perform() (1) settings_search = driver.find_element_by_class_name('setpref') settings_search.click() (3) ()
After opening the page, find the setting item according to the text information of the link, use the move_to_element() method of ActionChains to hover the mouse over the setting, and then find the search setting item
The right-click, double-click and drag events are all called by the above methods, just replace the methods. The drag event needs to be passed two parameters, the first parameter is the starting element of the drag and the second parameter is the end element
(driver).context_click("Positioning of right-clicked elements.").perform() #Right-click events (driver).double_click("Positioning of double-clicked elements.").perform() #DoubleClick event (driver).drag_and_drop("The starting element of the drag.", "Dragged end element").perform() #drag event
Keyboard Events
Almost all keyboard events are provided in the Keys class. Two keyboard events are used in the mouse events, the keyboard's key down (send_keys()) and the keyboard's enter event (send_keys()). The keyboard events need to be imported into the Keys module
from import Keys
All keyboard events are included in this module, send_keys is used to simulate keyboard input, in addition to the example can be used to simulate the keys on the keyboard, not only support for individual keyboard keys, but also support a combination of key inputs
# -*- coding: utf-8 -*- from selenium import webdriver import time driver = () ("") (3) driver.find_element_by_id('kw').send_keys('selenium') # Type "selenium" in the search box. driver.find_element_by_id('kw').send_keys() # Enter the space bar driver.find_element_by_id('kw').send_keys('python') # Type "python" into the search box. driver.find_element_by_id('kw').send_keys(, 'a') # Input Control+a to simulate full selection driver.find_element_by_id('kw').send_keys(, 'c') # Type Control+c to simulate copying driver.find_element_by_id('kw').send_keys(, 'v') # Type Control+v to simulate pasting driver.find_element_by_id('kw').send_keys() # Type enter instead of clicking the search button (3) ()
Here are some common keyboard events:
- Keys.BACK_SPACE: BackSpace key
- : Tab
- : Enter
- : Case conversion key (Shift)
- : Control key (Ctrl)
- : ALT key (Alt)
- : Return key (Esc)
- : Space
- Keys.PAGE_UP: Page Up
- Keys.PAGE_DOWN: Page Down
- : End-of-line key (End)
- : Header key (Home)
- : Left arrow key
- : Up arrow key
- : Right arrow key
- : Down arrow key
- : Insert key
- DELETE: Delete key (Delete)
- NUMPAD0 ~ NUMPAD9: Numeric Keys 1-9
- F1 ~ F12: F1 - F12 keys
- (, 'a'): key combination Control+a, select all
- (, 'c'): the key combination Control+c to copy the
- (, 'x'): the key combination Control+x, cuts the
- (, 'v'): key combination Control+v, paste
This is the whole content of this article.