In web automation, we may experience certain elements required to operate the keyboard or mouse, then we need to use the keyboard and mouse events, and today the keyboard and mouse operation a summary
Mouse Events
Mouse events need to introduce ActionChains class, you can view the source code to initialize see ActionChains need to pass the current session
Mouse steps:
1, storage mouse
2, an operation using the perform () method to perform operations
Commonly used mouse actions are:
move_to_element suspension
drag_and_drop drag operation
Double-click double_click
Right-click context_click
What specific needs, you can view the source code, very rich, very detailed and Kazakhstan. . .
For chestnut:
Baidu Home - Advanced Search
Sample code:
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains dr = webdriver.Chrome() dr.get("http://www.baidu.com") COL = (By.XPATH, ' // div [@ ID = "U1"] // A [@ class = "PF"] ' ) # element disposed positioning WebDriverWait (dr, 10, 0.5) .until (EC. visibility_of_element_located (COL)) # dominant presence waiting setting button element EL = dr.find_element (* COL) ActionChains (DR) .move_to_element (EL) .perform () # hovers setting button to CO.'S = (By.XPATH, ' // A [text () = "Advanced Search"] ' ) WebDriverWait(dr, 10, 0.5).until(EC.visibility_of_element_located(co)) dr.find_element ( * CO) .click () # click Advanced Search
Keyboard Events
We rarely use the keyboard events, or summarize it
The main event is a reference Keys keyboard class
Key combination:
send_keys(Keys.CONTROL,'a') 全选
send_keys (Keys.CONTROL, 'c') Copy
send_keys (Keys.CONTROL, 'v') Paste
send_keys (Keys.CONTROL, 'x') Shear
Non-key combinations:
Enter: Keys.ENTER
Delete: Keys.BACK_SPACE
Space: Keys.SPACE
Tabulation: Keys.TAB
Refresh: Keys.F5
For chestnuts
Baidu Home Enter key instead of the Search button by
from selenium import webdriver from selenium.webdriver.common.keys import Keys dr = webdriver.Chrome() dr.get("http://www.baidu.com") dr.implicitly_wait(3) dr.find_element(By.ID, "kw").send_keys("腾讯", Keys.ENTER)
These are summarized keyboard, mouse events