Showing posts with label Errors. Show all posts
Showing posts with label Errors. Show all posts

Thursday, April 10, 2025

Selenium Java - Warning/Errors and its solutions


Here's a list of standard warnings/errors in Selenium with Java and their respective solutions to help you debug faster and develop cleaner automation scripts.

✅ 1. org.openqa.selenium.NoSuchElementException

Cause: Trying to locate an element that does not exist on the page.

Solutions:

  • Ensure the element is present in the DOM and visible.

  • Add waits:

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
    
  • Check locator correctness (id, xpath, cssSelector, etc.).


✅ 2. ElementNotInteractableException

Cause: Element is present but cannot be interacted with (e.g., it's hidden or disabled).

Solutions:

  • Use WebDriverWait to ensure it is clickable:

    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));
    
  • Scroll into view using JavaScript:

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    

✅ 3. TimeoutException

Cause: Wait timed out before condition was met.

Solutions:

  • Increase wait time or optimize locator.

  • Use appropriate expected condition (e.g., presenceOfElementLocated vs visibilityOfElementLocated).


✅ 4. StaleElementReferenceException

Cause: WebElement is no longer attached to the DOM.

Solutions:

  • Re-locate the element after page reload or DOM change:

    element = driver.findElement(By.id("elementId"));
    
  • Use a try-catch block to retry locating the element.


✅ 5. SessionNotCreatedException

Cause: Browser version mismatch with WebDriver.

Solutions:

  • Make sure ChromeDriver/GeckoDriver matches your browser version.

  • Use WebDriverManager (for Maven):

    WebDriverManager.chromedriver().setup();
    

✅ 6. WebDriverException: unknown error: DevToolsActivePort file doesn't exist

Cause: Usually in headless mode with Chrome on Linux.

Solutions:

  • Add Chrome options:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--no-sandbox");
    options.addArguments("--disable-dev-shm-usage");
    options.addArguments("--headless");
    

✅ 7. Deprecation Warnings

Cause: Some older APIs (like Thread.sleep, implicitlyWait without Duration, etc.) are deprecated.

Solutions:

  • Replace deprecated code:

    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    

✅ 8. java.lang.NullPointerException

Cause: WebElement or WebDriver not initialized.

Solutions:

  • Check initialization:

    WebDriver driver = new ChromeDriver(); // Not null
    
  • Ensure correct scope in frameworks like TestNG, JUnit, etc.


✅ 9. org.openqa.selenium.ElementClickInterceptedException

Cause: Another element overlays the clickable element (like a modal or popup).

Solutions:

  • Wait for overlay to disappear.

  • Use JS click:

    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
    

✅ 10. IllegalStateException: The driver executable does not exist

Cause: Path to WebDriver not set correctly.

Solutions:

  • Set system property or use WebDriverManager:

    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");