Selenium Cheatsheet

WebDriver Basics

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

Browser Information

Property / MethodReturns
driver.current_urlCurrent page URL as string
driver.titleCurrent page <title> text
driver.page_sourceFull HTML source of the current page
driver.nameBrowser name ("chrome", "firefox", etc.)
print(driver.current_url)    # "https://example.com/login"
print(driver.title)          # "Login – Example"
print(driver.page_source)    # full HTML string

Window / Viewport Control

driver.maximize_window()
driver.minimize_window()
driver.fullscreen_window()
driver.set_window_size(1280, 800)
driver.set_window_position(0, 0)

size = driver.get_window_size()     # {"width": 1280, "height": 800}
pos  = driver.get_window_position() # {"x": 0, "y": 0}
rect = driver.get_window_rect()     # {"x": 0, "y": 0, "width": 1280, "height": 800}
driver.set_window_rect(x=0, y=0, width=1920, height=1080)

Script Execution

# Synchronous JavaScript
result = driver.execute_script("return document.title;")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.execute_script("arguments[0].click();", element)
driver.execute_script("arguments[0].value = arguments[1];", element, "text")

# Async JavaScript (waits for callback)
driver.execute_async_script("""
    var callback = arguments[arguments.length - 1];
    setTimeout(function() { callback('done'); }, 500);
""")

Timeouts

from selenium.webdriver.common.timeouts import Timeouts

# Set individual timeouts (seconds)
driver.implicitly_wait(10)                          # implicit wait (global)
driver.set_page_load_timeout(30)                    # max time for page.get()
driver.set_script_timeout(10)                       # max time for async scripts

# Selenium 4+ — set via Timeouts object
timeouts = Timeouts(implicit_wait=10, page_load=30, script=10)
driver.timeouts = timeouts

# Read current timeouts
print(driver.timeouts)

Prefer explicit waits (WebDriverWait) over implicitly_wait. Mixing both can cause unpredictable delays.

Cookies

# See alerts-and-cookies.md for full cookie reference
driver.get_cookies()                             # list of all cookies
driver.get_cookie("session_id")                  # single cookie dict or None
driver.add_cookie({"name": "foo", "value": "bar"})
driver.delete_cookie("foo")
driver.delete_all_cookies()

Local Storage / Session Storage

driver.execute_script("window.localStorage.setItem('key', 'value');")
value = driver.execute_script("return window.localStorage.getItem('key');")
driver.execute_script("window.localStorage.removeItem('key');")
driver.execute_script("window.localStorage.clear();")

Logging (Chrome)

from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability("goog:loggingPrefs", {"browser": "ALL", "performance": "ALL"})
driver = webdriver.Chrome(options=options)

logs = driver.get_log("browser")     # list of log entry dicts
for entry in logs:
    print(entry["level"], entry["message"])

Capabilities / Options Inspection

caps = driver.capabilities
print(caps["browserName"])    # "chrome"
print(caps["browserVersion"]) # "120.0.6099.109"
print(caps["platformName"])   # "linux"

Closing Sessions

driver.close()   # close the active tab; session remains if other tabs exist
driver.quit()    # terminate the entire browser session and kill the process