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 TypeMethodDescription
Clickclick(element)orclick()Clicks the specified element or the current mouse position.
Double Clickdouble_click(element)ordouble_click()Double-clicks the specified element or the current mouse position.
Right Clickcontext_click(element)orcontext_click()Right-clicks the specified element or the current mouse position.
Drag and Dropdrag_and_drop(source, target)Drags the source element and drops it onto the target element.
Hovermove_to_element(element)Moves the mouse to the specified element.
Press and Releaseclick_and_hold(element)andrelease()Holds down the specified element and releases it, used to implement drag operations.

Keyboard Operations

Operation TypeMethodDescription
Input Textsend_keys("text")Inputs the specified text into an input box or text area.
Press Combination Keyssend_keys(Keys.CONTROL + 'a')Simulates pressing combination keys (e.g., Ctrl + A).
Press a Single Keysend_keys(Keys.KEY_NAME)Simulates pressing a single key (e.g., Enter, Tab, Shift, etc.).
Release Keykey_up(key)Releases the pressed key.

Common Keyboard Key Values

Key ValueDescription
Keys.ENTEREnter key
Keys.TABTab key
Keys.SHIFTShift key
Keys.CONTROLCtrl key
Keys.ALTAlt key
Keys.ESCAPEEsc key
Keys.BACKSPACEBackspace key
Keys.SPACESpacebar
Keys.ARROW_UPUp arrow key
Keys.ARROW_DOWNDown arrow key
Keys.ARROW_LEFTLeft arrow key
Keys.ARROW_RIGHTRight 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

  1. Click: Simulates a left mouse button click operation.
  2. Double Click: Simulates a left mouse button double-click operation.
  3. Right Click: Simulates a 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 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 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 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 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 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 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 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 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 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 import webdriver
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()
Other Extensions