Jay Abhani
Senior Web Development Instructor at almaBetter
Explore our Selenium cheat sheet, covering key locators, commands and methods for web automation. Master Selenium with tips for Python, Java and C# programming
Selenium is a popular open-source framework for automating web applications. It's widely used for testing purposes but can also be applied for web scraping and other automation tasks. This comprehensive Selenium cheat sheet will guide you through the essential elements you need to know about Selenium, including its locators, commands, methods, and how to use Selenium with different programming languages like Python, Java, and C#.
Selenium is a suite of tools for automating web browsers. It includes several components:
In this cheat sheet, we will focus primarily on Selenium WebDriver and cover how to use it with Python, Java, and C#.
Locators are essential in Selenium as they are used to find elements on a web page for interaction. Here are the most common types of locators in Selenium:
The ID locator is the most efficient and commonly used way to find elements.
driver.find_element_by_id("elementId")
You can also locate an element by its name attribute.
driver.find_element_by_name("elementName")
If an element has a specific class, you can locate it using its class name.
driver.find_element_by_class_name("className")
This locator targets elements by their HTML tag name.
driver.find_element_by_tag_name("tagName")
This locator is used to find a link by the exact text it contains.
driver.find_element_by_link_text("ExactLinkText")
This works similarly to the link text locator but allows for partial matching.
driver.find_element_by_partial_link_text("PartialText")
driver.find_element_by_css_selector("cssSelector")
XPath is a very flexible way of locating elements, especially when CSS selectors don’t suffice.
driver.find_element_by_xpath("xpathExpression")
XPath (XML Path Language) is used for navigating through elements and attributes in an XML document. It is very useful for locating elements when CSS selectors aren’t enough.
Starts from the root element.
driver.find_element_by_xpath("/html/body/div/p")
Starts from a specific element.
driver.find_element_by_xpath("//div[@class='example']")
Find an element by its text content:
driver.find_element_by_xpath("//button[text()='Submit']")
Find an element by its partial text content:
driver.find_element_by_xpath("//button[contains(text(),'Sub')]")
Find elements by attribute:
driver.find_element_by_xpath("//input[@name='username']")
Find the nth child of an element:
driver.find_element_by_xpath("//ul/li[2]")
Python is one of the most popular programming languages for Selenium automation. Below is a quick guide to working with Selenium WebDriver in Python.
pip install selenium
from selenium import webdriver
driver = webdriver.Chrome(executable_path='path_to_chromedriver')
driver.get("https://example.com")
You can use different browser drivers with Selenium WebDriver. For instance:
driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver = webdriver.Edge()
Using WebDriverWait and expected_conditions is a common way to handle dynamic elements.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "elementId")))
Java is another popular language for Selenium automation. Below are some essential methods and commands you should know when using Selenium with Java.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>latest_version</version>
</dependency>
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
Java provides WebDriverWait for handling dynamic elements:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId")));
Here’s a list of commonly used Selenium methods to interact with web elements:
element.click()
element.send_keys("your input text")
text = element.text
attribute_value = element.get_attribute("attributeName")
url = driver.current_url
driver.switch_to.frame("frameName")
driver.quit()
Selenium offers a variety of commands to manage your browser session and interact with web elements.
driver.back()
driver.forward()
driver.refresh()
title = driver.title
driver.save_screenshot('screenshot.png')
alert = driver.switch_to.alert
alert.accept() # to accept
alert.dismiss() # to dismiss
The CSS Selector locator is an incredibly powerful tool when working with Selenium. Here's a quick guide on how to use it effectively.
driver.find_element_by_css_selector("tag#id")
driver.find_element_by_css_selector("tag.className")
driver.find_element_by_css_selector("input[name='username']")
driver.find_element_by_css_selector("div > p") # direct child
driver.find_element_by_css_selector("div p") # descendant
driver.find_element_by_css_selector("a:hover")
This detailed Selenium cheat sheet serves as a quick reference for all the key concepts and commands you’ll need while working with Selenium WebDriver. Whether you're using Selenium with Python, Java, or C#, the essential locators, methods, and commands listed here will provide you with the tools you need to get started with web automation and testing.
More Cheat Sheets and Top Picks