Troubleshooting Guide – Common errors and fixes - Nytrotype/Selenium-with-Java-automation-work GitHub Wiki

Introduction

Automation testing with Selenium + Java can introduce unexpected errors related to WebDriver compatibility, element identification, execution environments, and CI/CD integrations. This guide provides common Selenium errors with detailed fixes, helping ensure stable test execution across environments.


1. WebDriver Issues

❌ Error: SessionNotCreatedException: Unable to create a new session

Cause:

  • The installed browser version is incompatible with the WebDriver version.
  • Outdated ChromeDriver/GeckoDriver.

Fix:

  • Update ChromeDriver/GeckoDriver to match the browser version.
  • Download the latest ChromeDriver from: [ChromeDriver Downloads](https://chromedriver.chromium.org/downloads)
  • Example setup:
    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
  • If running headless, ensure WebDriver supports the version:
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");
    WebDriver driver = new ChromeDriver(options);

❌ Error: WebDriverException: The path to the driver executable must be set

Cause:

  • Missing WebDriver path in system properties or incorrect location.

Fix:

  • Ensure WebDriver binary (chromedriver.exe, geckodriver.exe) is placed in the correct folder.
  • Set driver path explicitly:
    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
  • If using Docker, confirm Selenium WebDriver binaries exist in the container.

❌ Error: Driver is not reachable

Cause:

  • WebDriver may not have sufficient permissions or may be blocked by firewall policies.

Fix:

  • Ensure administrator privileges are enabled for WebDriver execution.
  • Run WebDriver as an administrator (Right-click → Run as Administrator).
  • Check antivirus/firewall settings, as they might block the execution.

2. Locator & Element Issues

❌ Error: NoSuchElementException: Unable to locate element

Cause:

  • Incorrect XPath/CSS selectors.
  • The element is not visible or not present in the DOM.

Fix:

  • Verify selectors using browser Inspect Tool (F12).
  • Use Explicit Waits instead of direct element interaction:
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit")));
  • If inside an iframe, switch context:
    driver.switchTo().frame("frameName");

❌ Error: StaleElementReferenceException: Element is no longer attached

Cause:

  • The element was dynamically updated or removed from the DOM before interaction.

Fix:

  • Relocate the element before interacting:
    WebElement element = driver.findElement(By.id("button"));
    element.click(); // Re-fetch before clicking
  • Use Explicit Waits:
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
    wait.until(ExpectedConditions.elementToBeClickable(By.id("button"))).click();

❌ Error: ElementClickInterceptedException

Cause:

  • Another element is blocking the clickable element (e.g., pop-ups, overlays).

Fix:

  • Scroll into view before clicking:
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView();", element);
    element.click();
  • Handle pop-ups using JavaScript Executor:
    js.executeScript("document.getElementById('button').click();");

3. Browser & Execution Issues

❌ Error: TimeoutException: Timed out waiting for element

Cause:

  • The element took too long to load due to a slow application response.

Fix:

  • Increase Explicit Wait duration:
    new WebDriverWait(driver, Duration.ofSeconds(15))
        .until(ExpectedConditions.visibilityOfElementLocated(By.id("searchBox")));
  • Use Fluent Waits for better handling:
    Wait<WebDriver> fluentWait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(20))
        .pollingEvery(Duration.ofSeconds(2))
        .ignoring(NoSuchElementException.class);

❌ Error: Java heap space error during execution

Cause:

  • Insufficient memory allocation for Java execution.

Fix:

  • Increase JVM memory allocation:
    java -Xmx4G -Xms2G -jar selenium-tests.jar

4. CI/CD & Remote Execution Issues

❌ Error: org.openqa.selenium.remote.ProtocolHandshake: Could not establish a connection

Cause:

  • Selenium Grid or Cloud Execution (Sauce Labs, LambdaTest) isn't reachable.

Fix:

  • Ensure Selenium Hub URL is correct in remote WebDriver setup:
    String hubUrl = "http://localhost:4444/wd/hub";
    WebDriver driver = new RemoteWebDriver(new URL(hubUrl), new ChromeOptions());
  • Check firewall/network configurations that may block execution.

❌ Error: Test execution fails in CI/CD pipeline but runs locally

Cause:

  • Dependency issues, missing browser drivers, or unsupported execution environment.

Fix:

  • Verify browser drivers exist in CI/CD environment.
  • Run tests in headless mode:
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");
    WebDriver driver = new ChromeDriver(options);
  • Configure GitHub Actions pipeline to cache dependencies:
    - name: Cache Maven dependencies
      uses: actions/cache@v2
      with:
        path: ~/.m2/repository
        key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
        restore-keys: ${{ runner.os }}-maven-

5. Miscellaneous Issues

❌ Error: Selenium test execution is slow

Cause:

  • High CPU usage, improper waits, or redundant operations.

Fix:

  • Disable animations in Windows (Search "Performance Options" → Disable Visual Effects).
  • Optimize test execution with Parallel Testing:
    <suite name="Automation Suite" parallel="methods" thread-count="4">

❌ Error: Tests failing due to flaky elements

Fix:

  • Implement retry logic for unstable elements:
    @Test(retryAnalyzer = RetryAnalyzer.class)
    public void testExample() { ... }

Conclusion

This expanded troubleshooting guide provides fixes for common Selenium errors, ensuring smooth automation execution. By applying wait strategies, debugging browser issues, optimizing test execution, and improving CI/CD configurations, QA engineers/SDETs can maintain reliable test environments.

⚠️ **GitHub.com Fallback** ⚠️