Element Interaction Fundamentals
WebDriver provides intuitive methods for simulating user actions on page elements.
Basic Operations
| Method |
Description |
click() |
Triggers a single mouse click on the target element |
send_keys(*value) |
Simulates keystroke input into form fields |
clear() |
Removes existing text content from input fields |
submit() |
Submits the containing form, equivalent to pressing Enter within a form field. While occasionally interchangeable with click() on submit buttons, submit() only works within form contexts whereas click() works on any clickable element. |
Element State Verification
Before interacting with elements, validate their current state using these inspection methods:
| Method |
Return Value Description |
get_attribute(name) |
Retrieves the string value of the specified HTML attribute |
is_enabled() |
Boolean indicating if the element accepts user input |
is_selected() |
Boolean indicating if checkbox, radio button, or option element is current selected |
is_displayed() |
Boolean indicating if the element is visible to the user (not just in DOM) |
Advanced Mouse Actions
Complex mouse gestures require the ActionChains class, which queues multiple actions and executes them sequentially via the perform() method.
Available Gestures
| Action |
Description |
context_click(target) |
Simulates right-click on the specified element |
double_click(target) |
Simulates rapid double-click |
drag_and_drop(source, destination) |
Clicks and holds the source element, moves to destination, then releases |
move_to_element(target) |
Moves cursor to the center of the specified element (hover effect) |
perform() |
Executes all queued actions in the chain |
Implementation Example
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
# Initialize browser instance
browser = webdriver.Chrome()
browser.get("https://demo-site.com")
browser.maximize_window()
# Locate interactive elements
input_box = browser.find_element(By.CSS_SELECTOR, "#search-input")
menu_trigger = browser.find_element(By.CSS_SELECTOR, ".user-menu")
# Populate field
input_box.send_keys("automation testing")
time.sleep(2)
# Double-click to select text
ActionChains(browser).double_click(input_box).perform()
time.sleep(2)
# Right-click context menu
ActionChains(browser).context_click(input_box).perform()
time.sleep(2)
# Hover over navigation element
ActionChains(browser).move_to_element(menu_trigger).perform()
time.sleep(3)
browser.quit()
Keyboard Event Simulation
The Keys enumeration provides constants for special keyboard keys, enabling complex input patterns including modifier combinations.
Common Key Constants
| Key Constant |
Function |
Keys.BACK_SPACE |
Delete character before cursor |
Keys.SPACE |
Insert space character |
Keys.TAB |
Move focus to next field |
Keys.ESCAPE |
Press Escape key |
Keys.ENTER |
Press Return/Enter key |
Keys.CONTROL |
Control/Command modifier for shortcuts |
Keys.F1 through Keys.F12 |
Function keys |
Text Editing Shortcuts
| Combination |
Action |
Keys.CONTROL, 'a' |
Select all text |
Keys.CONTROL, 'c' |
Copy selection to clipboard |
Keys.CONTROL, 'x' |
Cut selection to clipboard |
Keys.CONTROL, 'v' |
Paste clipboard content |
Practical Implementation
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get("https://search-engine.com")
search_field = browser.find_element(By.NAME, "query")
# Type with intentional typo
search_field.send_keys("seleniumm")
# Correct typo using backspace
search_field.send_keys(Keys.BACK_SPACE)
# Add space and additional terms
search_field.send_keys(Keys.SPACE)
search_field.send_keys("webdriver")
# Select all text using Ctrl+A
search_field.send_keys(Keys.CONTROL, 'a')
# Cut the selection
search_field.send_keys(Keys.CONTROL, 'x')
# Paste it back (demonstrating clipboard operations)
search_field.send_keys(Keys.CONTROL, 'v')
# Submit search using Enter key instead of clicking button
search_field.send_keys(Keys.ENTER)
browser.quit()
Dropdown Menu Control
HTML <select> elements require specialized handling through the Select class, which provides methods for option manipulation that are more reliable then standard click interactions.
Import Statement
from selenium.webdriver.support.ui import Select
Selection Methods
| Method |
Selection Criterion |
select_by_index(index) |
Zero-based index of option |
select_by_value(value) |
Value of the value attribute |
select_by_visible_text(text) |
Exact text displayed to user |
Deselection (Multi-select Only)
| Method |
Purpose |
deselect_all() |
Clears all selected options |
deselect_by_index(index) |
Removes selection by index |
deselect_by_value(value) |
Removes selection by value attribute |
deselect_by_visible_text(text) |
Removes selection by displayed text |
Retrieval Properties
| Property |
Returns |
first_selected_option |
The first currently selected WebElement |
all_selected_options |
List of all selected WebElements |
options |
List of all available option WebElements |
is_multiple |
Boolean indicating if multiple selection is allowed |
Usage Pattern
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome()
browser.get("https://form-example.com")
# Initialize Select object
country_dropdown = browser.find_element(By.ID, "country-select")
selector = Select(country_dropdown)
# Select by visible label
selector.select_by_visible_text("United States")
# Select by value attribute
selector.select_by_value("CA")
# Select by position
selector.select_by_index(2)
# For multi-select elements only: deselect specific option
if selector.is_multiple:
selector.deselect_by_value("CA")
# Retrieve current selection
selected = selector.first_selected_option.text
all_options = [opt.text for opt in selector.options]