Selenium Mouse and Keyboard Operations
Selenium provides rich mouse and keyboard operation features that can simulate user interactions, such as clicking, double-clicking, dragging and dropping, hovering, entering text, pressing combination keys, etc.
The following are commonly used mouse and keyboard operations in Selenium and their descriptions:
Mouse Operations
| Operation Type | Method | Description |
|---|---|---|
| Click | click(element)orclick() | Clicks the specified element or the current mouse position. |
| Double Click | double_click(element)ordouble_click() | Double-clicks the specified element or the current mouse position. |
| Right Click | context_click(element)orcontext_click() | Right-clicks the specified element or the current mouse position. |
| Drag and Drop | drag_and_drop(source, target) | Drags the source element and drops it onto the target element. |
| Hover | move_to_element(element) | Moves the mouse to the specified element. |
| Press and Release | click_and_hold(element)andrelease() | Holds down the specified element and releases it, used to implement drag operations. |
Keyboard Operations
| Operation Type | Method | Description |
|---|---|---|
| Input Text | send_keys("text") | Inputs the specified text into an input box or text area. |
| Press Combination Keys | send_keys(Keys.CONTROL + 'a') | Simulates pressing combination keys (e.g., Ctrl + A). |
| Press a Single Key | send_keys(Keys.KEY_NAME) | Simulates pressing a single key (e.g., Enter, Tab, Shift, etc.). |
| Release Key | key_up(key) | Releases the pressed key. |
Common Keyboard Key Values
| Key Value | Description |
|---|---|
Keys.ENTER | Enter key |
Keys.TAB | Tab key |
Keys.SHIFT | Shift key |
Keys.CONTROL | Ctrl key |
Keys.ALT | Alt key |
Keys.ESCAPE | Esc key |
Keys.BACKSPACE | Backspace key |
Keys.SPACE | Spacebar |
Keys.ARROW_UP | Up arrow key |
Keys.ARROW_DOWN | Down arrow key |
Keys.ARROW_LEFT | Left arrow key |
Keys.ARROW_RIGHT | Right arrow key |
Mouse Operations (ActionChains Class)
In Selenium, mouse operations are primarily implemented through theActionChainsclass.ActionChainsThe class allows us to perform complex mouse operations, such as clicking, double-clicking, right-clicking, dragging and dropping, and hovering.
Click, Double Click, Right Click
- Click: Simulates a left mouse button click operation.
- Double Click: Simulates a left mouse button double-click operation.
- Right Click: Simulates a right mouse button click operation.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open the web page
driver.get("https://example.com")
# Locate the element
element = driver.find_element(By.ID, "element_id")
# Create an ActionChains object
actions = ActionChains(driver)
# Click operation
actions.click(element).perform()
# Double-click operation
actions.double_click(element).perform()
# Right-click operation
actions.context_click(element).perform()
# Close the browser
driver.quit()
Drag and Drop Operations
Drag and drop operation refers to dragging an element to another position or onto another element.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open the web page
driver.get("https://example.com")
# Locate the element to be dragged and the target element
source_element = driver.find_element(By.ID, "source_element_id")
target_element = driver.find_element(By.ID, "target_element_id")
# Create an ActionChains object
actions = ActionChains(driver)
# Drag and drop operation
actions.drag_and_drop(source_element, target_element).perform()
# Close the browser
driver.quit()
Hover Operations
Hover operation refers to hovering the mouse pointer over a certain element, usually to trigger a dropdown menu or display tooltip information.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# Initialize the browser driver
driver = webdriver.Chrome()
# Open the web page
driver.get("https://example.com")
# Locate the element
element = driver.find_element(By.ID, "element_id")
# Create an ActionChains object
actions = ActionChains(driver)
# Hover operation
actions.move_to_element(element).perform()
# Close the browser
driver.quit()
Keyboard Operations (Keys Class)
Keyboard operations in Selenium are primarily implemented through theKeysclass.KeysThe class provides simulated operations for various keyboard keys, including sending combination keys and special key operations.
Sending Combination Keys
Combination key operation refers to pressing multiple keys at the same time, such asCtrl + C(copy) orCtrl + V(paste).
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open the web page
driver.get("https://example.com")
# Locate the input box
input_element = driver.find_element(By.ID, "input_element_id")
# Enter content and send combination keys
input_element.send_keys("Hello, World!")
input_element.send_keys(Keys.CONTROL, 'a') # Select all
input_element.send_keys(Keys.CONTROL, 'c') # Copy
input_element.send_keys(Keys.CONTROL, 'v') # Paste
# Close the browser
driver.quit()
Special Key Operations (Enter, Tab, Shift, etc.)
Special key operation refers to simulating the press of special keys on the keyboard, such asEnter、Tab、Shiftetc.
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open the web page
driver.get("https://example.com")
# Locate the input box
input_element = driver.find_element(By.ID, "input_element_id")
# Enter content and press the Enter key
input_element.send_keys("Hello, World!")
input_element.send_keys(Keys.ENTER)
# Press the Tab key to switch to the next element
input_element.send_keys(Keys.TAB)
# Press the Shift key and enter uppercase letters
input_element.send_keys(Keys.SHIFT, "a") # Enter an uppercase 'A'
# Close the browser
driver.quit()
Comprehensive Example
The following is a complete example demonstrating how to use mouse and keyboard operations:
Example
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
# Set the correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open the web page
driver.get("https://www.example.com")
# Mouse operations
element = driver.find_element(By.ID, "button")
actions = ActionChains(driver)
actions.move_to_element(element).click().perform()
# Keyboard operations
input_box = driver.find_element(By.ID, "input-box")
input_box.send_keys("Hello, Selenium!")
input_box.send_keys(Keys.ENTER)
# Close the browser
driver.quit()