Contents

Selenium Cheat Sheet

Info
This post assumes prior knowledge of python and selenium 4

Installation

Selenium: pip install selenium

Download the drivers, make sure that your browser, selenium and driver versions are compatible with each other.

Instantiate your driver

Chrome

1
2
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:/webdrivers/chromedriver.exe")

With custom options

1
2
3
4
5
6
7
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
## option to not show an open browser window
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path="C:/webdrivers/chromedriver.exe", options=options)

Mozilla

1
2
from selenium import webdriver
driver = webdriver.Firefox(executable_path="C:/webdrivers/geckodriver.exe")

With custom options

1
2
3
4
5
6
7
8
9
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
# option for firefox.exe location, if firefox is not installed in default location
options.binary_location = "C:/.../firefox.exe"
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
driver = webdriver.Firefox(executable_path="C:/webdrivers/geckodriver.exe", options=options)

Edge

1
2
from selenium import webdriver
driver = webdriver.Edge(executable_path="C:/webdrivers/msedgedriver.exe")

With custom options

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium import webdriver
from selenium.webdriver.edge import service

options = webdriver.EdgeOptions()
options.use_chromium = True
options.add_argument("start-maximized")
# option for msegde.exe location, if edge is not installed in default location
options.binary_location = r"C:/../msedge.exe"
service = service.Service("C:/webdrivers/msedgedriver.exe")
driver = webdriver.Edge(service=service, options=options)

Open a website

1
driver.get("https://google.com")

Find an element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium.webdriver.common.by import By

element = find_element(By.ID, "ID")
element = find_element(By.NAME, "NAME")
element = find_element(By.XPATH, "XPATH")
element = find_element(By.LINK_TEXT, "TEXT")
element = find_element(By.PARTIAL_LINK_TEXT, "PARTIAL_TEXT")
element = find_element(By.TAG_NAME, "TAG_NAME")
element = find_element(By.CLASS_NAME, "CLASS_NAME")
element = find_element(By.CSS_SELECTOR, "CSS_SELECTOR")

Find multiple elements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium.webdriver.common.by import By

elements = find_elements(By.ID, "ID")
elements = find_elements(By.NAME, "NAME")
elements = find_element(By.XPATH, "XPATH")
elements = find_element(By.LINK_TEXT, "TEXT")
elements = find_element(By.PARTIAL_LINK_TEXT, "PARTIAL_TEXT")
elements = find_element(By.TAG_NAME, "TAG_NAME")
elements = find_element(By.CLASS_NAME, "CLASS_NAME")
elements = find_element(By.CSS_SELECTOR, "CSS_SELECTOR")

Get information from element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
element.text
# get an attribute like href, class, value etc
element.get_attribute("ATTRIBUTE")
# get property like text_length
element.get_property("PROPERTY")
# is element visible to user
element.is_displayed()
element.is_enabled()
element.is_selected()
element.screenshot("/Screenshots/foo.png")

Interactions

Button

1
element.cick()

Text

1
2
3
4
element.send_keys("lorem ipsum")
# press arrow key
element.send_keys("lorem ipsum", Keys.ARROW_DOWN)
element.clear()

List of all keys is at: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys

Options

1
2
3
4
5
6
7
8
9
from selenium.webdriver.support.ui import Select

select = Select(driver.find_element(By.XPATH, "XPATH"))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)
options = select.options # get all options
select.all_selected_options # get all selected options
select.deselect_all() # deselect all selected options

Submit a form

1
element.submit()

Waits

Explicit waits

Wait for some time until certain condition is true. In the example, Selenium will wait for a maximum of 10s, checking the condition every 500ms. TimeoutException is thrown if element is not found after 10s.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "ID"))
    )
finally:
    driver.quit()

All supported expected conditions are at: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

Implicit waits

Wait for some time before finding any element. This applies for any find, unless webdriver is created anew. Use this carefully, because it can slow down the script execution.

1
driver.implicitly_wait(10)

Close

1
driver.close()

Reference