liminfo

Selenium Reference

Free reference guide: Selenium Reference

41 results

About Selenium Reference

This Selenium Reference is a searchable cheat sheet for Selenium WebDriver with Python, organized into eight categories: Driver, Selectors, Actions, Waits, Frames, Alerts, Screenshots, and Configuration. Every entry includes a production-ready code snippet that you can paste into your test suite or automation script. Whether you are writing end-to-end tests with pytest, building a web scraper, or automating repetitive form submissions, this page puts the most-used WebDriver patterns within instant reach.

Selenium WebDriver communicates with browsers through a standardized protocol, controlling Chrome, Firefox, Edge, and Safari programmatically. The learning curve often centers on two pain points: choosing the right selector strategy (ID vs CSS vs XPath) and handling asynchronous page loads with explicit waits. This reference addresses both topics with multiple entries, showing expected_conditions patterns like presence_of_element_located, visibility_of_element_located, and element_to_be_clickable that prevent flaky tests caused by timing issues.

The reference also covers advanced interaction patterns — ActionChains for hover menus and drag-and-drop, Select wrapper for dropdown manipulation, iframe switching for embedded content, and JavaScript execution via execute_script for scrolling and DOM manipulation. Configuration entries explain headless mode, page load strategies, cookie management, and custom Service paths, giving you the setup knowledge needed for both local development and CI/CD pipeline execution.

Key Features

  • Driver initialization for Chrome and Firefox with ChromeOptions for headless mode and custom arguments
  • Six selector strategies — By.ID, By.CSS_SELECTOR, By.XPATH, By.NAME, By.CLASS_NAME, and By.LINK_TEXT — with find_element and find_elements
  • Explicit wait patterns using WebDriverWait with expected_conditions for presence, visibility, clickability, text, and URL changes
  • ActionChains for complex interactions including hover, click chains, and context menus
  • Select wrapper for dropdown elements with select_by_visible_text, select_by_value, and select_by_index
  • Frame and window switching — switch_to.frame(), switch_to.window(), switch_to.default_content(), and parent_frame navigation
  • Alert handling with accept, dismiss, send_keys, and alert_is_present wait condition
  • Screenshot capture at page and element level, plus execute_script for JavaScript-based scrolling and DOM queries

Frequently Asked Questions

Which Selenium version does this reference cover?

The examples target Selenium 4.x for Python, which uses the modern WebDriver API. Key differences from Selenium 3 include the Service class for driver paths (instead of executable_path as a direct argument), the By-based locator strategy, and built-in support for Chrome DevTools Protocol.

When should I use CSS selectors vs XPath?

CSS selectors are faster and more readable for most cases — targeting IDs, classes, attributes, and structural relationships. Use XPath when you need to match by text content (//button[text()="Submit"]), navigate upward to parent elements, or use complex conditional logic that CSS cannot express.

What is the difference between implicit and explicit waits?

Implicit waits set a global timeout for find_element calls to poll the DOM before raising NoSuchElementException. Explicit waits (WebDriverWait + expected_conditions) wait for a specific condition on a specific element, offering much finer control. Mixing both is discouraged because it can lead to unpredictable timeout behavior.

How do I handle pages that load content dynamically with JavaScript?

Use WebDriverWait with an appropriate expected_condition. For AJAX content, wait for EC.presence_of_element_located or EC.visibility_of_element_located. For single-page apps that change the URL hash, use EC.url_contains. Avoid time.sleep() as it creates brittle tests with arbitrary delays.

How do I run Selenium tests in headless mode?

Add --headless to ChromeOptions: options.add_argument("--headless"). For CI/CD pipelines, also add --no-sandbox and --disable-dev-shm-usage to prevent resource issues in containerized environments. Headless mode runs without a visible browser window but supports all WebDriver operations including screenshots.

How do I interact with elements inside an iframe?

First switch to the iframe context using driver.switch_to.frame(element) where the element is the iframe tag. Perform your actions, then return to the main page with driver.switch_to.default_content(). For nested iframes, use switch_to.parent_frame() to step up one level at a time.

Why do my tests fail intermittently with StaleElementReferenceException?

This occurs when the DOM element you located has been re-rendered or detached from the page. Common fixes include re-locating the element after page changes, using WebDriverWait to wait for the updated element, or wrapping interactions in try/except blocks with retry logic. It frequently happens after navigation, AJAX updates, or framework re-renders.

Can I use this reference for Selenium with Java or JavaScript?

The API patterns and concepts (selectors, waits, ActionChains, frame switching) are identical across all Selenium language bindings. Only the import paths and syntax differ. The Python examples here map directly to equivalent Java (WebDriverWait, ExpectedConditions) and JavaScript (until, By) code.