DEV Community

Cover image for 5 Selenium Debugging Patterns That Save Me Hours
Akhil Vijay
Akhil Vijay

Posted on

5 Selenium Debugging Patterns That Save Me Hours

Flaky tests and "element not found" errors eat more QA time than writing the tests in the first place. After a lot of red builds, these are the patterns I reach for first.

1. Check the wait before the locator

Most NoSuchElementException errors are timing problems, not wrong locators. Replace time.sleep() with an explicit wait for the exact condition you need:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 15)
btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#save")))
btn.click()
Enter fullscreen mode Exit fullscreen mode

Also avoid mixing implicit and explicit waits, because the timeouts can stack in unpredictable ways.

2. Write locators that survive UI changes

Auto-generated IDs and hashed class names break between builds. Prefer stable attributes, and ask your developers for data-testid attributes. If an ID has a changing part, match on the stable part:

driver.find_element(By.CSS_SELECTOR, "[id^='user_']")   # starts with
driver.find_element(By.CSS_SELECTOR, "[id*='email']")   # contains
Enter fullscreen mode Exit fullscreen mode

3. Re-find stale elements

A StaleElementReferenceException means the page re-rendered and your saved element no longer exists. Don't reuse old references after an action that refreshes the DOM. Find the element again right before you use it.

4. Capture evidence when a test fails

Don't guess. Save a screenshot and the page source automatically on failure:

def capture(driver, name):
    driver.save_screenshot(f"{name}.png")
    with open(f"{name}.html", "w", encoding="utf-8") as f:
        f.write(driver.page_source)
Enter fullscreen mode Exit fullscreen mode

In Robot Framework, a teardown does the same job: Run Keyword If Test Failed Capture Page Screenshot.

5. Check for iframes, windows and overlays

If an element is visible on screen but Selenium can't find it, it's usually inside an iframe:

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe#payment")))
# ... interact ...
driver.switch_to.default_content()
Enter fullscreen mode Exit fullscreen mode

If a click fails with ElementClickInterceptedException, something is covering the element, such as a cookie banner or modal. Wait for it to close, or scroll the element into view.

A quick failure-to-fix map

What you see First fix to try
NoSuchElementException Explicit wait, check iframe, verify the locator in DevTools
StaleElementReferenceException Re-find the element right before using it
ElementClickInterceptedException Wait for the overlay to close, scroll into view
Works locally, fails in CI Set the window size, compare with a headless run

Want all of this in one place?

I packed these patterns, plus Selenium core concepts, dynamic element handling, Robot Framework essentials and quick reference tables, into a compact 9-page PDF:

Selenium + Robot Framework Field Guide

Which of these has cost you the most time? Tell me in the comments and I'll write about it next.

Top comments (1)

Collapse
 
akhil_vijay_baaf761e59956 profile image
Akhil Vijay •

Which of these has cost you the most time?