Bytes
Web Development

Selenium Cheat Sheet: Locators, Commands, Methods, Webdriver

Last Updated: 5th March, 2025
icon

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#.

Introduction to Selenium

Selenium is a suite of tools for automating web browsers. It includes several components:

  • Selenium WebDriver: The most commonly used component for automating browsers.
  • Selenium Grid: Allows you to run tests on different machines and browsers in parallel.
  • Selenium IDE: A Chrome and Firefox extension for recording and playback.
  • Selenium RC (Remote Control): The old version, which has been largely replaced by WebDriver.

In this cheat sheet, we will focus primarily on Selenium WebDriver and cover how to use it with Python, Java, and C#.

Selenium Locators Cheat Sheet

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:

By ID

The ID locator is the most efficient and commonly used way to find elements.

    driver.find_element_by_id("elementId")

By Name

You can also locate an element by its name attribute.

    driver.find_element_by_name("elementName")

By Class Name

If an element has a specific class, you can locate it using its class name.

    driver.find_element_by_class_name("className")

By Tag Name

This locator targets elements by their HTML tag name.

    driver.find_element_by_tag_name("tagName")

By Link Text

This locator is used to find a link by the exact text it contains.

    driver.find_element_by_link_text("ExactLinkText")

By Partial Link Text

This works similarly to the link text locator but allows for partial matching.

    driver.find_element_by_partial_link_text("PartialText")

By CSS Selector

    driver.find_element_by_css_selector("cssSelector")

By XPath

XPath is a very flexible way of locating elements, especially when CSS selectors don’t suffice.

    driver.find_element_by_xpath("xpathExpression")

Selenium XPath Cheat Sheet

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.

Basic XPath Syntax

Absolute XPath

Starts from the root element.

    driver.find_element_by_xpath("/html/body/div/p")

Relative XPath

Starts from a specific element.

    driver.find_element_by_xpath("//div[@class='example']")

Common XPath Expressions

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 Selenium Cheat Sheet

Python is one of the most popular programming languages for Selenium automation. Below is a quick guide to working with Selenium WebDriver in Python.

Setting Up Selenium WebDriver in Python

    pip install selenium
    from selenium import webdriver

    driver = webdriver.Chrome(executable_path='path_to_chromedriver')
    driver.get("https://example.com")

Handling Different Browser Drivers

You can use different browser drivers with Selenium WebDriver. For instance:

ChromeDriver:

    driver = webdriver.Chrome()

FirefoxDriver:

    driver = webdriver.Firefox()

EdgeDriver:

    driver = webdriver.Edge()

Waiting for Elements

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")))

Selenium Java Cheat Sheet

Java is another popular language for Selenium automation. Below are some essential methods and commands you should know when using Selenium with Java.

Setting Up Selenium WebDriver in 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");

Waiting for Elements

Java provides WebDriverWait for handling dynamic elements:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId")));

Selenium Methods Cheat Sheet

Here’s a list of commonly used Selenium methods to interact with web elements:

Click an Element

    element.click()

Send Keys to an Input Field

    element.send_keys("your input text")

Get Text from an Element

    text = element.text

Get an Element's Attribute Value

    attribute_value = element.get_attribute("attributeName")

Get the Current URL

    url = driver.current_url

Switch to a Different Frame

    driver.switch_to.frame("frameName")

Close the Browser

    driver.quit()

Selenium Commands Cheat Sheet

Selenium offers a variety of commands to manage your browser session and interact with web elements.

Navigating Back and Forward

    driver.back()
    driver.forward()

Refresh the Page

    driver.refresh()

Get Page Title

    title = driver.title

Take a Screenshot

    driver.save_screenshot('screenshot.png')

Handle Alerts

    alert = driver.switch_to.alert
    alert.accept()  # to accept
    alert.dismiss()  # to dismiss

CSS Selector Selenium Cheat Sheet

The CSS Selector locator is an incredibly powerful tool when working with Selenium. Here's a quick guide on how to use it effectively.

Basic Syntax

    driver.find_element_by_css_selector("tag#id")
    driver.find_element_by_css_selector("tag.className")

Attribute-based CSS Selectors

    driver.find_element_by_css_selector("input[name='username']")

Child and Descendant Selectors

    driver.find_element_by_css_selector("div > p")  # direct child
    driver.find_element_by_css_selector("div p")  # descendant

Using Pseudo-classes

    driver.find_element_by_css_selector("a:hover")

Conclusion

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

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2025 AlmaBetter