Selenium WebDriver Cheat Sheet: Locators, Waits, and Actions (Python and Java)
Edited by Dev Kilaru, Senior Data Analyst. How we make our content
Introduction
Selenium WebDriver automates real browsers. This cheat sheet shows the same operations in Python and Java for Selenium 4, where Selenium Manager downloads matching browser drivers for you. Check the official Selenium documentation for your version.
Prerequisites
- Python or Java basics
- Chrome or another supported browser installed
Step-by-Step Guide
Step 1: Install and start a browser
With Selenium 4.6 and newer, Selenium Manager finds or downloads the right driver automatically.
# Python
pip install selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
print(driver.title, driver.current_url)
driver.quit()
// Java (add the selenium-java dependency)
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
System.out.println(driver.getTitle());
driver.quit();Step 2: Locators
Prefer id, name, and CSS selectors. XPath is powerful but more fragile.
# Python
from selenium.webdriver.common.by import By
driver.find_element(By.ID, "username")
driver.find_element(By.NAME, "email")
driver.find_element(By.CSS_SELECTOR, "form .btn-primary")
driver.find_element(By.XPATH, "//button[text()='Save']")
driver.find_element(By.LINK_TEXT, "Sign in")
driver.find_elements(By.CLASS_NAME, "item") # list of elements
// Java
driver.findElement(By.id("username"));
driver.findElement(By.cssSelector("form .btn-primary"));
driver.findElement(By.xpath("//button[text()='Save']"));
driver.findElements(By.className("item"));Step 3: Element actions
These work on any WebElement you have found.
# Python
el = driver.find_element(By.ID, "q")
el.clear()
el.send_keys("selenium")
el.click()
el.text
el.get_attribute("value")
el.is_displayed(); el.is_enabled(); el.is_selected()
from selenium.webdriver.common.keys import Keys
el.send_keys(Keys.ENTER)
// Java
el.clear(); el.sendKeys("selenium"); el.click();
el.getText(); el.getAttribute("value");
el.isDisplayed(); el.isEnabled();Step 4: Waits
Use explicit waits for the exact condition you need. Avoid mixing them with implicit waits, and avoid sleep.
# Python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, "save")))
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".toast")))
wait.until(EC.url_contains("dashboard"))
// Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("save")));
wait.until(ExpectedConditions.urlContains("dashboard"));Step 5: Dropdowns, frames, windows, and alerts
Switch context before interacting with a frame, a new window, or an alert.
# Python
from selenium.webdriver.support.ui import Select
Select(driver.find_element(By.ID, "country")).select_by_visible_text("India")
driver.switch_to.frame("payment-frame")
driver.switch_to.default_content()
handles = driver.window_handles
driver.switch_to.window(handles[-1])
alert = driver.switch_to.alert
alert.accept() # or alert.dismiss(), alert.text
// Java
new Select(driver.findElement(By.id("country"))).selectByVisibleText("India");
driver.switchTo().frame("payment-frame");
driver.switchTo().defaultContent();
driver.switchTo().alert().accept();Step 6: Mouse and keyboard actions, options, and screenshots
ActionChains builds complex interactions. Browser options control headless mode and window size.
# Python
from selenium.webdriver import ActionChains
ActionChains(driver).move_to_element(menu).click(item).perform()
ActionChains(driver).double_click(el).perform()
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
driver.save_screenshot("page.png")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")FAQs
- Why do my Selenium tests fail intermittently?
- Most flaky failures come from acting before the page is ready. Replace sleeps with explicit waits for the condition you need, and prefer stable locators such as ids and data attributes.
Summary
Start a driver with Selenium Manager, choose stable locators, use explicit waits instead of sleeps, switch context for frames, windows and alerts, and use ActionChains and options when needed.
Related tutorials
- Shell Scripting Cheat Sheet: Bash Variables, Tests, Loops, Functions, and Text Tools
- Linux Command Line Cheat Sheet: Files, Permissions, Processes, and Networking
- Docker and kubectl Cheat Sheet: Containers, Images, Compose, and Kubernetes
- SQL Cheat Sheet: SELECT, Joins, Aggregates, and Data Changes
- Git Cheat Sheet: Everyday Commands, Branching, and Undoing Mistakes
Comments
Log in or create a free account to join the discussion.