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 / Method | Returns |
|---|---|
driver.current_url | Current page URL as string |
driver.title | Current page <title> text |
driver.page_source | Full HTML source of the current page |
driver.name | Browser 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) overimplicitly_wait. Mixing both can cause unpredictable delays.
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