Selenium Mouse And Keyboard Operation
Selenium provides rich mouse and keyboard operation functions that can simulate user interaction behaviors, such as click, double-click, drag and drop, hover, input text, press key combinations, etc.
The following are the commonly used mouse and keyboard operations in Selenium and their descriptions:
### Mouse Operations
| **Operation Type** | **Method** | **Description** |
| --- | --- | --- |
| **Click** | `click(element)` or `click()` | Click on the specified element or current mouse position. |
| **Double Click** | `double_click(element)` or `double_click()` | Double-click on the specified element or current mouse position. |
| **Right Click** | `context_click(element)` or `context_click()` | Right-click on the specified element or current mouse position. |
| **Drag and Drop** | `drag_and_drop(source, target)` | Drag the source element to the target element. |
| **Hover** | `move_to_element(element)` | Move the mouse to the specified element. |
| **Click and Hold** | `click_and_hold(element)` and `release()` | Click and hold the specified element, then release, used to implement drag operations. |
### Keyboard Operations
| **Operation Type** | **Method** | **Description** |
| --- | --- | --- |
| **Input Text** | `send_keys("text")` | Input specified text into the input box or text area. |
| **Press Key Combination** | `send_keys(Keys.CONTROL + 'a')` | Simulate pressing a key combination (such as Ctrl + A). |
| **Press Single Key** | `send_keys(Keys.KEY_NAME)` | Simulate pressing a single key (such as Enter, Tab, Shift, etc.). |
| **Release Key** | `key_up(key)` | Release 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` | Space key |
| `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 mainly implemented through the `ActionChains` class. The `ActionChains` class allows us to perform complex mouse operations such as click, double-click, right-click, drag and drop, and hover.
### Click, Double Click, Right Click
1. **Click**: Simulate left mouse button click operation.
2. **Double Click**: Simulate left mouse button double-click operation.
3. **Right Click**: Simulate right mouse button click operation.
## Example
from selenium import webdriver
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 correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open webpage
driver.get("https://example.com")
# Locate Element
element = driver.find_element(By.ID,"element_id")
# Create ActionChains Object
actions = ActionChains(driver)
# Click Action
actions.click(element).perform()
# Double-Click Action
actions.double_click(element).perform()
# RightKeyClick Action
actions.context_click(element).perform()
# Close browser
driver.quit()
### Drag and Drop Operation
Drag and drop operation refers to dragging an element to another position or another element.
## Example
from selenium import webdriver
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 correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open webpage
driver.get("https://example.com")
# Locate Draggable and Target Elements
source_element = driver.find_element(By.ID,"source_element_id")
target_element = driver.find_element(By.ID,"target_element_id")
# Create ActionChains Object
actions = ActionChains(driver)
# Drag and Drop Action
actions.drag_and_drop(source_element, target_element).perform()
# Close browser
driver.quit()
### Hover Operation
Hover operation refers to hovering the mouse pointer over an element, usually used to trigger dropdown menus or display tooltip information.
## Example
from selenium import webdriver
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 Browser Driver
driver = webdriver.Chrome()
# Open webpage
driver.get("https://example.com")
# Locate Element
element = driver.find_element(By.ID,"element_id")
# Create ActionChains Object
actions = ActionChains(driver)
# Hover Action
actions.move_to_element(element).perform()
# Close browser
driver.quit()
## Keyboard Operations (Keys Class)
Keyboard operations in Selenium are mainly implemented through the `Keys` class. The `Keys` class provides various keyboard key simulation operations, including sending key combinations and special key operations.
### Send Key Combinations
Key combination operation refers to pressing multiple keys at the same time, such as `Ctrl + C` (copy) or `Ctrl + V` (paste).
## Example
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# Set correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open webpage
driver.get("https://example.com")
# Locate Input Field
input_element = driver.find_element(By.ID,"input_element_id")
# Enter content and send key combinations
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 browser
driver.quit()
### Special Key Operations (Enter, Tab, Shift, etc.)
Special key operations refer to simulating pressing special keys on the keyboard, such as `Enter`, `Tab`, `Shift`, etc.
## Example
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# Set correct driver path
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Open webpage
driver.get("https://example.com")
# Locate Input Field
input_element = driver.find_element(By.ID,"input_element_id")
# Enter content and press Enter Key
input_element.send_keys("Hello, World!")
input_element.send_keys(Keys.ENTER)
# Press Tab Key to switch to the next element
input_element.send_keys(Keys.TAB)
# Press Shift Key and type uppercase letters
input_element.send_keys(Keys.SHIFT,"a")# Input Uppercase Text 'A'
# Close browser
driver.quit()
* * *
## Comprehensive Example
The following is a complete example demonstrating how to use mouse and keyboard operations:
## Example
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.web
YouTip