Selenium Cheatsheet
Alerts and Cookies
Use this Selenium reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
JavaScript Alerts
Types of Alerts
| Dialog Type | alert() | confirm() | prompt() |
|---|---|---|---|
| Text visible | Yes | Yes | Yes |
| Has OK button | Yes | Yes | Yes |
| Has Cancel button | No | Yes | Yes |
| Accepts input | No | No | Yes |
Switching to an Alert
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait for alert to appear wait = WebDriverWait(driver, 10) wait.until(EC.alert_is_present()) alert = driver.switch_to.alert
Alert Methods
| Method | Description |
|---|---|
alert.text | Get the alert message text |
alert.accept() | Click OK / Accept |
alert.dismiss() | Click Cancel / Dismiss |
alert.send_keys(text) | Type into a prompt() dialog |
# Simple alert — just accept alert = driver.switch_to.alert print(alert.text) # "Are you sure?" alert.accept() # Confirm dialog — accept or dismiss alert.accept() # click OK alert.dismiss() # click Cancel # Prompt dialog — type a value then accept alert.send_keys("my input text") alert.accept()
Full Alert Pattern
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import UnexpectedAlertPresentException driver.find_element(By.ID, "delete-btn").click() try: WebDriverWait(driver, 5).until(EC.alert_is_present()) alert = driver.switch_to.alert print(f"Alert says: {alert.text}") alert.accept() except: print("No alert appeared")
Unexpected Alerts
from selenium.common.exceptions import UnexpectedAlertPresentException try: driver.find_element(By.ID, "submit").click() except UnexpectedAlertPresentException as e: print(f"Unexpected alert: {e.alert_text}") driver.switch_to.alert.accept()
Local Storage and Session Storage
# Local Storage driver.execute_script("localStorage.setItem('key', 'value');") val = driver.execute_script("return localStorage.getItem('key');") driver.execute_script("localStorage.removeItem('key');") driver.execute_script("localStorage.clear();") # Session Storage driver.execute_script("sessionStorage.setItem('key', 'value');") val = driver.execute_script("return sessionStorage.getItem('key');") driver.execute_script("sessionStorage.clear();") # Get all keys keys = driver.execute_script("return Object.keys(localStorage);")
Gotchas
You must be on the correct domain before adding cookies.
add_cookie()silently ignores cookies for other domains without raising an error.
httpOnlycookies cannot be read or set via JavaScript (includinglocalStorageworkarounds). Selenium'sadd_cookie()is the only way to inject them.
alert.send_keys()followed byalert.dismiss()types into a prompt and then cancels — the typed value is not submitted.
UnexpectedAlertPresentExceptionis raised by some drivers if an alert is open when you try to interact with the page. Always dismiss alerts before continuing.