Selenium Cheatsheet

Interactions

Use this Selenium reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Basic Element Interactions

MethodDescription
element.click()Left-click the element
element.send_keys(*value)Type text or send key presses
element.clear()Clear the current value of an input
element.submit()Submit the form containing the element
from selenium.webdriver.common.by import By

btn   = driver.find_element(By.ID, "submit")
field = driver.find_element(By.NAME, "username")

btn.click()
field.clear()
field.send_keys("alice")
field.submit()

Reading Element State

Property / MethodReturns
element.textVisible text content
element.get_attribute("attr")Attribute/JS-property blend (e.g. live value); None if neither exists
element.get_dom_attribute("attr")Attribute exactly as written in the HTML; None if absent from the markup
element.get_property("prop")JavaScript property value
element.tag_nameHTML tag name ("input", "div", …)
element.is_displayed()True if visible
element.is_enabled()True if interactable
element.is_selected()True for checkboxes/radios/options
print(element.text)                        # "Click me"
print(element.get_attribute("href"))       # "https://..."
print(element.get_attribute("class"))      # "btn btn-primary"
print(element.get_attribute("value"))      # current input value
print(element.get_attribute("innerHTML"))  # inner HTML
print(element.get_attribute("outerHTML"))  # outer HTML

# Difference: attribute vs property
# For <input type="checkbox" checked>:
el.get_attribute("checked")      # "true" (string)
el.get_property("checked")       # True (bool)

# get_dom_attribute reads only the static HTML markup
# For <input value="initial"> after the user types "edited":
el.get_dom_attribute("value")    # "initial" (as authored in HTML)
el.get_attribute("value")        # "edited" (falls through to the live property)
el.get_dom_attribute("missing")  # None (attribute not in the markup)

CSS Values

color  = element.value_of_css_property("color")
font   = element.value_of_css_property("font-size")
display = element.value_of_css_property("display")

Element Geometry

loc  = element.location          # {"x": 120, "y": 340}
size = element.size              # {"width": 200, "height": 50}
rect = element.rect              # {"x": 120, "y": 340, "width": 200, "height": 50}

Scrolling

# Scroll element into view (JavaScript)
driver.execute_script("arguments[0].scrollIntoView(true);", element)

# Scroll to specific coordinates
driver.execute_script("window.scrollTo(0, 500);")

# Scroll to bottom of page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

# Smooth-scroll to center (still plain JS injection)
driver.execute_script("arguments[0].scrollIntoView({behavior:'smooth',block:'center'});", element)

# Selenium 4.2+ — true WebDriver-protocol scroll (see Actions API topic)
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).scroll_to_element(element).perform()

Keyboard Keys (Special Keys)

from selenium.webdriver.common.keys import Keys

field.send_keys(Keys.RETURN)
field.send_keys(Keys.ENTER)
field.send_keys(Keys.TAB)
field.send_keys(Keys.ESCAPE)
field.send_keys(Keys.BACK_SPACE)
field.send_keys(Keys.DELETE)
field.send_keys(Keys.SPACE)
field.send_keys(Keys.ARROW_DOWN)
field.send_keys(Keys.ARROW_UP)
field.send_keys(Keys.PAGE_DOWN)
field.send_keys(Keys.HOME)
field.send_keys(Keys.END)
field.send_keys(Keys.F5)

# Combinations
field.send_keys(Keys.CONTROL, "a")   # Ctrl+A (select all)
field.send_keys(Keys.CONTROL, "c")   # Ctrl+C (copy)
field.send_keys(Keys.CONTROL, "v")   # Ctrl+V (paste)
field.send_keys(Keys.COMMAND, "a")   # Cmd+A (macOS)

Force-Click with JavaScript

# Use when a normal click is blocked by an overlay or element is off-screen
driver.execute_script("arguments[0].click();", element)

Hover (Mouse Over)

from selenium.webdriver.common.action_chains import ActionChains

menu_item = driver.find_element(By.ID, "dropdown-trigger")
ActionChains(driver).move_to_element(menu_item).perform()

Drag and Drop

source = driver.find_element(By.ID, "drag-source")
target = driver.find_element(By.ID, "drop-target")

ActionChains(driver).drag_and_drop(source, target).perform()

Right-Click (Context Menu)

ActionChains(driver).context_click(element).perform()

Double-Click

ActionChains(driver).double_click(element).perform()

Gotchas

element.clear() does not fire input or change events in some frameworks (React, Angular). Use send_keys(Keys.CONTROL + "a", Keys.DELETE) or a JS approach to trigger them.

click() can raise ElementClickInterceptedException if another element (toast, modal, cookie banner) is on top. Dismiss the overlay first or use the JS click fallback.

is_displayed() returns False for visibility:hidden and display:none but True for opacity:0. Use CSS property checks or JS for opacity-hidden elements.