catalogue
Selenium provides some methods for mouse and keyboard operations, such as mouse hovering, sliding, keyboard input, etc. follow me to learn about them.
1 mouse events
In Selenium, the methods about mouse operation are encapsulated in the ActionChains class.
method | explain |
ActionChains(driver) | Construct ActionChains object |
context_click() | Perform a mouse over operation |
move_to_element(above) | Right click |
double_click() | double-click |
drag_and_drop() | drag |
move_to_element(above) | Perform a mouse over operation |
context_click() | Used to simulate the right mouse button operation. The element position needs to be specified when calling |
perform() | Executing all the actions stored in ActionChains can be understood as submitting the entire operation |
Here is an example to see how to use. Take Baidu home page as an example. If you move the mouse pointer to the setting menu bar option on baidu home page, a drop-down menu bar will appear, as shown in the following figure. If you remove the mouse, it will disappear. The following uses Selenium to simulate.
First, you need to locate the "Settings" location. Here we use XPath to locate:
The codes are as follows:
from selenium.webdriver import Chrome from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains web = Chrome() #Visit Baidu Homepage web.get('https://www.baidu.com/') #Navigate to the element to be suspended above = web.find_element(by=By.XPATH, value='//*[@id="s-usersetting-top"]') #Perform a mouse over operation ActionChains(web).move_to_element(above).perform()
The operation effect is as follows:
From the previous code, we can see that the key is ActionChains (WEB) move_to_element(above). Use the ActionChains class to call move_to_element(above) suspends the event, and then executes the perform() method to submit the action.
2 keyboard events
send_ The Keys() method can be used to simulate keyboard input. In addition, we can also use it to input keys on the keyboard, or even key combinations. The Keys() class provides methods for almost all keys on the keyboard, as shown in the following figure.
method | explain |
send_keys(Keys.BACK_SPACE) | Delete key |
send_keys(Keys.SPACE) | Space bar |
send_keys(Keys.TAB) | Tab key |
send_keys(Keys.ESCAPE) | backspace key |
send_keys(Keys.ENTER) | enter key |
send_keys(Keys.CONTROL,'a') | select all |
send_keys(Keys.CONTROL,'c') | copy |
send_keys(Keys.CONTROL,'x') | shear |
send_keys(Keys.CONTROL,'v') | paste |
send_keys(Keys.F1) | F1 |
send_keys(Keys.F2) | F2 |
Take Baidu as an example.
The codes are as follows:
from selenium.webdriver import Chrome from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import time web = Chrome() #Visit Baidu Homepage web.get('https://www.baidu.com/') #Find the input box and input the Dragon Boat Festival 6 web.find_element(by=By.ID, value="kw").send_keys("Dragon Boat Festival 6") time.sleep(2) #Delete multiple input 6 web.find_element(by=By.ID, value="kw").send_keys(Keys.BACK_SPACE) time.sleep(2) #Enter the space bar + "Dragon Boat Racing“ web.find_element(by=By.ID, value="kw").send_keys(Keys.SPACE) web.find_element(by=By.ID, value="kw").send_keys("Dragon-boat Racing") time.sleep(2) #Select all input box contents web.find_element(by=By.ID, value="kw").send_keys(Keys.CONTROL,'a') time.sleep(2) #Cut input box contents web.find_element(by=By.ID, value="kw").send_keys(Keys.CONTROL,'x') time.sleep(2) #Paste content into input box web.find_element(by=By.ID, value="kw").send_keys(Keys.CONTROL,'v') time.sleep(2) #ENTER to click web.find_element(by=By.ID, value="kw").send_keys(Keys.ENTER)
The operation effect is as follows:
Mouse and keyboard events