Troubleshooting Guide – Common errors and fixes - Nytrotype/Selenium-with-Java-automation-work GitHub Wiki
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.
✅ 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);
✅ 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.
✅ 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.
✅ 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");
✅ 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();
✅ 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();");
✅ 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);
✅ Cause:
- Insufficient memory allocation for Java execution.
✅ Fix:
- Increase JVM memory allocation:
java -Xmx4G -Xms2G -jar selenium-tests.jar
✅ 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.
✅ 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-
✅ 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">
✅ Fix:
- Implement retry logic for unstable elements:
@Test(retryAnalyzer = RetryAnalyzer.class) public void testExample() { ... }
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.