Selenium Cheat Sheet

Selenium is an open-source automation testing tool that is widely used for web application testing. It is a suite of tools that includes Selenium IDE, Selenium WebDriver, and Selenium Grid. Selenium is compatible with multiple programming languages such as Java, Python, C#, Ruby, and JavaScript.

Selenium IDE is a record and playback tool that allows testers to record their actions on a web application and replay them to test the application’s functionality. Selenium WebDriver is a powerful tool that allows testers to write scripts in their preferred programming language to automate web application testing. Selenium Grid is a tool that allows testers to run tests on multiple machines and browsers simultaneously.

Selenium is widely used in the software industry due to its flexibility, ease of use, and compatibility with multiple programming languages. It is also highly customizable, allowing testers to create their own test frameworks and integrate them with other tools.

This cheat sheet provides an extensive list of Selenium commands and their descriptions. The commands are divided into different categories for easy reference.

Table of Contents

Getting Started

CommandDescription
from selenium import webdriverImport the Selenium webdriver module
driver = webdriver.Chrome()Create a new Chrome webdriver instance
driver.get(url)Navigate to a URL
driver.quit()Close the browser window and end the session

Locators

CommandDescription
driver.find_element_by_id(id)Find an element by its ID attribute
driver.find_element_by_name(name)Find an element by its name attribute
driver.find_element_by_xpath(xpath)Find an element using an XPath expression
driver.find_element_by_css_selector(css_selector)Find an element using a CSS selector
driver.find_element_by_class_name(class_name)Find an element by its class name
driver.find_element_by_tag_name(tag_name)Find an element by its tag name

Interacting with Elements

CommandDescription
element.click()Click on an element
element.send_keys(keys)Send keys to an element
element.clear()Clear the contents of an input field
element.get_attribute(attribute)Get the value of an attribute
element.textGet the text of an element
element.is_displayed()Check if an element is displayed
element.is_enabled()Check if an element is enabled
element.is_selected()Check if an element is selected

Navigation

CommandDescription
driver.back()Navigate back to the previous page
driver.forward()Navigate forward to the next page
driver.refresh()Refresh the current page

Alerts and Pop-ups

CommandDescription
alert = driver.switch_to.alertSwitch to an alert dialog
alert.accept()Accept an alert dialog
alert.dismiss()Dismiss an alert dialog
alert.send_keys(keys)Send keys to an alert dialog

Frames and Windows

CommandDescription
driver.switch_to.frame(frame)Switch to a frame
driver.switch_to.default_content()Switch back to the default content
driver.window_handlesGet a list of window handles
driver.switch_to.window(handle)Switch to a window by its handle

Wait Commands

CommandDescription
from selenium.webdriver.support.ui import WebDriverWaitImport the WebDriverWait module
wait = WebDriverWait(driver, timeout)Create a new WebDriverWait instance
wait.until(condition)Wait until a condition is met
EC.presence_of_element_located(locator)Wait for an element to be present
EC.visibility_of_element_located(locator)Wait for an element to be visible
EC.element_to_be_clickable(locator)Wait for an element to be clickable

Actions Class

CommandDescription
from selenium.webdriver.common.action_chains import ActionChainsImport the ActionChains module
actions = ActionChains(driver)Create a new ActionChains instance
actions.move_to_element(element)Move the mouse to an element
actions.click()Click the mouse
actions.double_click()Double-click the mouse
actions.context_click()Right-click the mouse
actions.drag_and_drop(source, target)Drag and drop an element

JavaScript Execution

CommandDescription
driver.execute_script(script)Execute a JavaScript script
driver.execute_async_script(script)Execute an asynchronous JavaScript script

Browser Management

CommandDescription
driver.maximize_window()Maximize the browser window
driver.set_window_size(width, height)Set the size of the browser window
driver.get_cookies()Get the current cookies
driver.add_cookie(cookie)Add a new cookie
driver.delete_cookie(name)Delete a cookie by its name
driver.delete_all_cookies()Delete all cookies

Reference