sodcalculator

10 Tech How-To

Fix Chrome Browser Issues in Selenium

Guide 10-21331 · Updated · Step by step

If you’re automating Chrome with Selenium and hitting crashes, timeouts, or “Chrome failed to start” errors, this guide walks you through the most common fixes. You’ll learn how to align ChromeDriver versions, configure browser options, handle headless mode, and resolve stale element or connection issues.

What You’ll Need

  • Selenium WebDriver installed (Python, Java, C#, or JavaScript)
  • Chrome browser installed on your machine
  • ChromeDriver binary downloaded (or managed via WebDriverManager)
  • Basic knowledge of your programming language’s Selenium syntax

Step-by-Step

  1. Check Chrome and ChromeDriver versions. Open Chrome, go to Help > About Chrome, note the version (e.g., 126.0.6478.126). Download the matching ChromeDriver from the official site or use a driver manager. Mismatched versions cause immediate failures.
  2. Set the driver path correctly. In your code, specify the exact path to the ChromeDriver executable. For Python: webdriver.Chrome(executable_path='/path/to/chromedriver'). For newer Selenium versions, use Service object. Avoid relative paths unless you’re sure of the working directory.
  3. Add essential Chrome options. Common options to prevent crashes:
    • --headless=new for headless mode (use the new headless for better compatibility).
    • --no-sandbox if running as root or in a container.
    • --disable-dev-shm-usage to fix shared memory issues in Docker.
    • --disable-gpu for older systems or when GPU causes crashes.
    • --window-size=1920,1080 to avoid viewport issues.
  4. Use WebDriverManager (recommended). Instead of manual downloads, use a library like webdriver-manager (Python/Java) or selenium-manager (built into Selenium 4.6+). It automatically matches the driver version to your Chrome.
  5. Wait for elements properly. Replace hard-coded time.sleep() with explicit waits: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'myId'))). This reduces flakiness and timeouts.
  6. Handle pop-ups and multiple windows. If your script interacts with new tabs or alerts, switch contexts: driver.switch_to.window(driver.window_handles[-1]) or driver.switch_to.alert.accept(). Ignoring these causes element-not-found errors.
  7. Clean up after each run. Always call driver.quit() in a finally block to close all browser processes. Zombie Chrome processes can interfere with the next run.
  8. Run in headless mode for CI/CD. On servers without a display, use headless. Ensure you include --headless=new and possibly --no-sandbox if running as root.

Common Mistakes

  • Version mismatch: The #1 cause. Chrome updates automatically, but ChromeDriver doesn’t. Always sync them.
  • Using deprecated options: For example, --disable-extensions is no longer needed; --headless=old may not work with newer Chrome. Keep options minimal.
  • Not closing the driver: Leaving drivers open leads to memory leaks and “session not created” errors on the next run.
  • Hard waits: time.sleep(5) is slow and unreliable. Use explicit waits instead.
  • Incorrect path separators on Windows: Use raw strings or double backslashes: r'C:\path\chromedriver.exe'.

Tips

  • Use a virtual display (like Xvfb) on Linux if you can’t use headless but need a display.
  • Enable ChromeDriver logs for debugging: service = Service(log_output=subprocess.STDOUT).
  • Keep your Selenium library updated. Selenium 4 has better built-in driver management.
  • For parallel testing, create a new driver instance per thread, not a shared one.
  • If you’re behind a corporate proxy, set --proxy-server option.

Troubleshooting

Chrome fails to start (“Chrome crashed”) – Check the version match. If using Docker, add --no-sandbox and --disable-dev-shm-usage. Try running Chrome manually with the same options to see if it opens.

SessionNotCreatedException – This usually means the driver and browser versions are incompatible. Update both. Also, ensure no other Chrome instance is using the same user data directory.

Element not found even though it’s visible – The element may be inside an iframe. Switch to the iframe first: driver.switch_to.frame('frameName'). Or use a more specific selector.

Timeouts on page load – Increase the page load timeout: driver.set_page_load_timeout(30). Or set --disable-page-load-strategy to ‘eager’ to not wait for all resources.

ChromeDriver executable needs to be in PATH – Add the directory to your system PATH, or use WebDriverManager to avoid this entirely.

Headless mode works locally but not on CI – Ensure the CI environment has all required libraries (e.g., libnss3, libx11). Use a pre-built Docker image like selenium/standalone-chrome.

Frequently Asked Questions

Q: Do I need to download ChromeDriver every time Chrome updates?
A: Yes, if you’re manually managing it. Use WebDriverManager or Selenium Manager to automate the version matching.

Q: What’s the difference between headless and headful mode?
A: Headless runs without a visible UI, which is faster and works on servers. Headful shows the browser, useful for debugging. Use --headless=new for the latest Chrome.

Q: Why does my script work in normal mode but fail in headless?
A: Headless may have different viewport size, user agent, or GPU support. Set window size explicitly and add --disable-gpu if needed.

Q: How do I fix “cannot find Chrome binary”?
A: Set the path to the Chrome executable with options.binary_location = '/path/to/chrome'. This is common on Linux where Chrome is installed in a non-standard location.

Q: Can I run multiple Chrome sessions simultaneously?
A: Yes, but each needs its own driver instance and ideally a unique user-data-dir. Use --user-data-dir=/tmp/unique to avoid conflicts.

Q: What should I do if ChromeDriver is blocked by antivirus?
A: Add an exception for the ChromeDriver executable in your antivirus, or use a trusted driver manager that downloads from the official source.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →