Skip to main content
Quick Reference

Selenium Cheatsheet

Selenium 4 on one page — locators, waits, frames, and the traps around stale elements, attributes, and mixed waits.

10 sections 10 tables 14 code snippets

Open and find

First match

driver.get("https://example.com/cart")
item = driver.find_element(By.ID, "item")

Every match

rows = driver.find_elements(By.CSS_SELECTOR, "tr.item")

The session

CallMeaning
webdriver.Chrome()Starts Chrome. With no driver path, Selenium Manager resolves the driver
getLoads the URL and waits for the document load event, not for later script
find_elementThe first match. No match raises NoSuchElementException
find_elementsEvery match. No match is an empty list
withThe context manager calls quit when the block ends
BindingThese samples are the Python API. The same ideas exist in the other language bindings

This page is Selenium 4 in Python. Cypress is left out.

Locators

Both classes

# By.CSS_SELECTOR, ".qty.big"

Which strategy

ByMatches
IDThe id attribute. The first node wins if the page repeats an id
CSS_SELECTORA CSS selector. .a.b means both classes on one element
CLASS_NAMEOne class token. A space in the value is an invalid selector
XPATHAn XPath expression. A positional index starts at 1
LINK_TEXTAn a whose full visible text equals the string. PARTIAL_LINK_TEXT is a substring
NAMEThe name attribute

Waits

Visible and enabled

wait = WebDriverWait(driver, timeout=10)
wait.until(EC.element_to_be_clickable((By.ID, "buy")))

What you are waiting for

WaitUntil
WebDriverWaitPolls until the condition is true, or raises TimeoutException
presenceThe node is in the DOM. It can still be hidden
visibilityDisplayed, with width and height above 0
clickableVisible and enabled
implicitA session-wide pause on find calls only. The default is 0
sleepA fixed pause. The waiting guide says to use an explicit wait instead

Reading an element

The live value

el.get_property("value")

Three different reads

CallReturns
textVisible text. Text inside a hidden node is left out
get_propertyThe live DOM property, such as the current input value
get_dom_attributeThe attribute written in the HTML, not what the user typed
get_attributeThe property when one exists, otherwise the attribute
is_displayedWhether the element is shown
is_enabledWhether the control can take input

Click and type

Replace the field

el.clear()
el.send_keys("2")

A real select

Select(box).select_by_visible_text("Red")

What the call does

CallEffect
clickClicks when the element is visible, enabled, and not covered
send_keysAppends keys. Existing text stays unless you clear first
clearEmpties an input or textarea
SelectOnly a select element. A div menu raises UnexpectedTagNameException
select_by_visible_textThe option whose visible label matches
submitSubmits the form that contains the element

Frames and dialogs

Search inside the iframe

driver.switch_to.frame("pay")

Confirm the dialog

driver.switch_to.alert.accept()

Where the driver is looking

SwitchEffect
frameLater finds run inside that iframe. The top document is not searched
default_contentBack to the top document
parent_frameUp one frame
alertThe open dialog. accept confirms it. dismiss cancels it
windowswitch_to.window takes a handle. A new tab does not take focus by itself
Missed frameAn element inside an iframe raises NoSuchElementException from the top document

Action chains

Hover, then click

ActionChains(driver).move_to_element(menu).click().perform()

A gesture

PieceMeaning
ActionChainsStores the steps. Nothing is sent until perform
move_to_elementScrolls the element into view and moves the pointer onto it
clickClicks at the pointer after the move
drag_and_dropPresses on the source, moves, and releases on the target
pauseA gap in the chain, given in seconds
Next gestureBuild a new chain. perform does not start a fresh sequence for you

The driver

Quit when the block ends

with webdriver.Chrome() as driver:
    driver.get("https://example.com")

Lifetime

PieceMeaning
quitCloses every window and ends the session
closeCloses the current window. The session stays if another window is open
OptionsBrowser arguments, including headless, are set before the driver starts
Selenium ManagerRuns when no driver path is set. An explicit Service path is used instead
implicitly_waitApplies to later find calls for the rest of the session
New Chrome()A new empty browser. Cookies from the previous driver are gone

When the page moves

The node was replaced

# StaleElementReferenceException means find the element again

Exceptions worth naming

SymptomCause
StaleElementReferenceExceptionThe DOM node was replaced after you stored the element
ElementClickInterceptedExceptionAnother element covers the click point
ElementNotInteractableExceptionThe element is hidden or disabled
TimeoutExceptionThe explicit condition never became true
Empty find_elementsWith an implicit wait set, the call uses the full timeout before it returns []
Old referenceWaiting on a stored element does not search the page again

Interview traps

The Selenium 4 call

# find_element(By.ID, "item")

One argument to the condition

# EC.element_to_be_clickable((By.ID, "buy"))

Traps worth memorising

TrapWhat happensPrefer
find_element_by_idRemoved in Selenium 4find_element(By.ID, "item")
EC with two argumentsThe condition takes one locator tuple(By.ID, "buy")
presence, then clickThe node can be in the DOM and still be hiddenelement_to_be_clickable
implicit plus explicitThe timeouts stack, so the pause is longer than either numberAn explicit wait, with implicit left at 0
get_attribute("value")Returns the live property when that property existsget_dom_attribute for the HTML, get_property for the live value
A script clickexecute_script can click a control the user cannot reachelement.click()

How to use this page: find with By, wait for the condition you mean, and quit the driver when the flow ends.