WebDriver Commands & Browser Actions
Selenium's WebDriver API provides a comprehensive set of commands to interact with web pages — navigating URLs, clicking, typing, scrolling, and extracting information. Knowing which command to use in each situation, and understanding what can go wrong, is the difference between stable and flaky test automation.
Essential WebDriver Commands
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
# ── NAVIGATION ────────────────────────────────────────────────
driver.get("https://example.com") # Navigate to URL
driver.back() # Browser back button
driver.forward() # Browser forward button
driver.refresh() # Reload current page
print(driver.current_url) # Get current URL
print(driver.title) # Get page title
# ── FINDING & INTERACTING ─────────────────────────────────────
element = driver.find_element(By.ID, "email")
element.click() # Click element
element.clear() # Clear input field
element.send_keys("alice@example.com") # Type text
element.send_keys(Keys.RETURN) # Press Enter key
print(element.text) # Get visible text
print(element.get_attribute("value")) # Get attribute value
print(element.is_displayed()) # Is element visible?
print(element.is_enabled()) # Is element interactable?
# ── SCROLLING ────────────────────────────────────────────────
# Scroll page down
driver.execute_script("window.scrollTo(0, 1000)")
# Scroll element into view
driver.execute_script("arguments[0].scrollIntoView(true)", element)
# Scroll to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
# ── JAVASCRIPT EXECUTION ──────────────────────────────────────
# Click via JavaScript (use when element is hidden or overlapped)
driver.execute_script("arguments[0].click()", element)
# Get inner text
text = driver.execute_script("return arguments[0].innerText", element)
# Set value directly (for date pickers, readonly fields)
driver.execute_script("arguments[0].value='2026-01-15'", date_input)
# ── ADVANCED INTERACTIONS (hover, drag and drop) ──────────────
actions = ActionChains(driver)
# Hover over element (reveals dropdown)
actions.move_to_element(driver.find_element(By.ID, "menu")).perform()
# Double-click
actions.double_click(element).perform()
# Right-click (context menu)
actions.context_click(element).perform()
# Drag and drop
source = driver.find_element(By.ID, "draggable")
target = driver.find_element(By.ID, "droppable")
actions.drag_and_drop(source, target).perform()Common Mistakes
- Clicking stale elements — if the page reloads or DOM updates after finding an element, re-find it before clicking to avoid StaleElementReferenceException
- Not clearing fields before sending keys — if an input has pre-existing text, send_keys APPENDS; always call .clear() first
- Using execute_script for regular clicks — only use JS click for legitimately hidden/overlapping elements; regular clicks provide better coverage
- Failing silently on hidden elements — is_displayed() returns False for hidden elements; add assertions to verify visibility before interaction
Tip
Tip
Practice WebDriver Commands Browser Actions in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Testing pyramid: many unit tests, fewer integration, fewest E2E
Practice Task
Note
Practice Task — (1) Write a working example of WebDriver Commands Browser Actions from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with WebDriver Commands Browser Actions is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready software testing code.
Key Takeaways
- Selenium's WebDriver API provides a comprehensive set of commands to interact with web pages — navigating URLs, clicking, typing, scrolling, and extracting information.
- Clicking stale elements — if the page reloads or DOM updates after finding an element, re-find it before clicking to avoid StaleElementReferenceException
- Not clearing fields before sending keys — if an input has pre-existing text, send_keys APPENDS; always call .clear() first
- Using execute_script for regular clicks — only use JS click for legitimately hidden/overlapping elements; regular clicks provide better coverage