a preface
The core idea of Web UI Automation is to find elements, and then manipulate the two elements. In the Python Selenium series (ii) Element positioning method article, which describes how to find an element, this article describes the second item, how to manipulate the elements that have already been found.
Classification of two operation methods
Overall, the operation can be broadly divided into four categories, namely browser operation, keyboard operation, mouse operation, JS script .
1. How to use the browser:
Method |
Describe |
Driver.maximize_window () |
window maximized |
driver.back () |
page return |
Driver.forward () |
Page forward |
2. Keyboard Common operation method:
Send_keys () # keys analog Keyboard input, analog keyboard keys, key combinations, etc.
some of the keys are listed below:
alt= u ' \ue00a '
arrow_down= u ' \ue015 '
arrow_left= u ' \ue012 '
arrow_right= u ' \ue014 '
arrow_up= u ' \ue013 '
backspace= u ' \ue003 '
back_space= u ' \ue003 '
cancel= u ' \ue001 '
clear= u ' \ue005 '
command= u ' \ue03d '
control= u ' \ue009 '
3. Mouse Common operation method:
Click () # Actionchains Analog mouse operation, in addition to the most commonly used click, there are right click, double-click, etc.
Method |
Describe |
Click (on_element=none) |
Click left mouse button |
click_and_hold (on_element=none) |
Click left mouse button, do not release |
Context_click (on_element=none) |
right mouse button |
Double_click (on_element=none) |
Double-click the left mouse button |
Drag_and_drop (source, target) |
Drag to an element and release |
Drag_and_drop_by_offset (source,xoffset,yoffset) |
Drag to a coordinate and release |
Key_down (value, element=none) |
Press the key on one of the keyboards |
key_up (value, element=none) |
release a key |
Move_by_offset (xoffset, yoffset) |
mouse moved from current position to a coordinate |
move_to_element (to_element) |
mouse moves to an element |
Move_to_element_with_offset (to_element, Xoffset, Yoffset) |
move to the position of how far away from an element (upper-left corner coordinates) |
perform () |
All actions in the execution chain |
release (on_element=none) |
release the left mouse button at an element location |
Send_keys (*keys_to_send) |
send a key to the current focus element |
Send_keys_to_element (element, *keys_to_send) |
Sends a key to the specified element |
4. Call the JS script:
Execute_script # Execute JS script to complete specific actions
three operation Practical Example
1. Browser Operation Example
2. Keyboard Operation Example
3. Mouse Operation Example
4. Script action Example
four Additional knowledge points supplement
actionchains (browser). Double_click (Click_button). Perform () # Mouse Double-click Operation Example
in the mouse operation, notice that the Actionchains (Driver). Whenthe method is called, the action is not executed immediately, but all operations are stored sequentially in a queue, and when the Perform () method is called , the time in the queue executes sequentially. So when you use the mouse, you need to call the Perform () method to complete the execution of the action.
five Other resources
about thepython Seleniumfor a video explanation of how the elements are commonly used, see: http://i.youku.com/weiworld521 SectionThe Festival.
Python Selenium Series (iii) common operation types and methods