SELENIUM WEBDRIVER QUICK REFERENCE
Browser automation, element interaction, waits, and assertions
Setup
Install
pip install selenium webdriver-manager
# webdriver-manager auto-downloads browser drivers
Basic Driver Setup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()))
Headless Mode
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=options)
Supported Browsers
| webdriver.Chrome() | Google Chrome / Chromium |
| webdriver.Firefox() | Mozilla Firefox (GeckoDriver) |
| webdriver.Edge() | Microsoft Edge (Chromium) |
| webdriver.Safari() | Apple Safari (macOS only) |
Browser & Navigation
Navigation
driver.get("https://example.com")
driver.back() # browser back
driver.forward() # browser forward
driver.refresh() # reload page
Browser Properties
| driver.title | Current page title |
| driver.current_url | Current page URL |
| driver.page_source | Full page HTML source |
| driver.get_cookies() | List all cookies |
Window Management
driver.set_window_size(1920, 1080)
driver.maximize_window()
driver.minimize_window()
driver.quit() # close all windows, end session
Finding Elements
Locator Strategies
from selenium.webdriver.common.by import By
driver.find_element(By.ID, "login-btn")
driver.find_element(By.CLASS_NAME, "nav-item")
driver.find_element(By.CSS_SELECTOR, "div.card > h2")
driver.find_element(By.XPATH, "//input[@name='q']")
By Strategies
| By.ID | Match element id attribute |
| By.NAME | Match element name attribute |
| By.CLASS_NAME | Match CSS class (single class) |
| By.TAG_NAME | Match HTML tag name |
| By.CSS_SELECTOR | CSS selector (most flexible) |
| By.XPATH | XPath expression |
| By.LINK_TEXT | Exact anchor text |
| By.PARTIAL_LINK_TEXT | Partial anchor text match |
Find Multiple
items = driver.find_elements(By.CSS_SELECTOR, "li.item")
for item in items:
print(item.text)
# Returns empty list if none found (no exception)
Interacting
Click & Type
elem = driver.find_element(By.ID, "search")
elem.clear() # clear existing text
elem.send_keys("selenium python")
elem.submit() # submit parent form
Dropdowns
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.ID, "country"))
select.select_by_visible_text("Canada")
select.select_by_value("ca")
select.select_by_index(2)
Element Properties
| .text | Visible text content |
| .get_attribute('href') | HTML attribute value |
| .is_displayed() | True if element is visible |
| .is_enabled() | True if element is interactive |
| .is_selected() | True if checkbox/radio is selected |
| .tag_name | HTML tag (e.g. 'input', 'div') |
| .value_of_css_property('color') | Computed CSS property value |
Waits
Explicit Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "result")))
Expected Conditions
| presence_of_element_located | Element exists in DOM |
| visibility_of_element_located | Element is visible on page |
| element_to_be_clickable | Element is visible and enabled |
| text_to_be_present_in_element | Element contains expected text |
| alert_is_present | JavaScript alert is showing |
| staleness_of | Element is no longer in DOM |
| title_contains | Page title contains text |
Implicit Wait
driver.implicitly_wait(10) # seconds, applies globally
# Explicit waits are preferred — more precise control
Frames & Windows
Frames
driver.switch_to.frame("frame-name") # by name/id
driver.switch_to.frame(0) # by index
driver.switch_to.frame(elem) # by element
driver.switch_to.default_content() # back to main
Windows & Tabs
original = driver.current_window_handle
driver.switch_to.new_window("tab") # open new tab
driver.switch_to.window(original) # switch back
driver.close() # close current tab
Alerts
alert = driver.switch_to.alert
print(alert.text)
alert.accept() # click OK
alert.dismiss() # click Cancel
alert.send_keys("input text")
Screenshots
Capture Screenshots
driver.save_screenshot("page.png") # full page
elem = driver.find_element(By.ID, "chart")
elem.screenshot("chart.png") # single element
Screenshot as Base64
b64 = driver.get_screenshot_as_base64()
png = driver.get_screenshot_as_png() # bytes
Actions
Action Chains
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(menu).click().perform()
Keyboard Actions
from selenium.webdriver.common.keys import Keys
elem.send_keys(Keys.ENTER)
elem.send_keys(Keys.CONTROL, "a") # select all
actions.key_down(Keys.SHIFT).click(elem).perform()
Mouse Actions
| .click(elem) | Click element |
| .double_click(elem) | Double-click element |
| .context_click(elem) | Right-click element |
| .move_to_element(elem) | Hover over element |
| .drag_and_drop(src, dst) | Drag source to destination |
| .click_and_hold(elem) | Press and hold mouse button |
| .release() | Release mouse button |
Assertions
Common Assertions (pytest)
assert "Dashboard" in driver.title
assert driver.find_element(By.ID, "msg").text == "Done"
assert driver.current_url.endswith("/home")
assert len(driver.find_elements(By.CSS_SELECTOR, "tr")) > 0
Wait-Based Assertions
WebDriverWait(driver, 5).until( # appears
EC.visibility_of_element_located((By.ID, "success")))
WebDriverWait(driver, 5).until( # disappears
EC.invisibility_of_element_located((By.ID, "spinner")))
JavaScript Execution
result = driver.execute_script("return document.title")
driver.execute_script(
"arguments[0].scrollIntoView(true);", elem)
Common Patterns
Page Object Pattern
class LoginPage:
URL = "/login"
user_loc = (By.ID, "username")
def login(self, drv, user, pwd):
drv.find_element(*self.user_loc).send_keys(user)
Context Manager
from selenium import webdriver
with webdriver.Chrome() as driver:
driver.get("https://example.com")
print(driver.title)
# driver.quit() called automatically
Retry & Cleanup
try:
driver.get("https://example.com")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "btn")))
finally: driver.quit()