WebDriver Selenium API: ElementNotFoundErrorException when Element is clearly there !

前端 未结 3 456
旧时难觅i
旧时难觅i 2020-12-24 03:37

sometimes when running tests on WebDriver with Javascript turned off, WebDriver crashes due to an ElementNotFound Error when it finds an element, and attempts to click it.

3条回答
  •  借酒劲吻你
    2020-12-24 04:39

    Fluent Wait - Best approach as it's the most flexible and configurable on the fly (has ignore exceptions option, polling every, timeout):

    public Wait getFluentWait() {
        return new FluentWait<>(this.driver)
                .withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS)
                .pollingEvery(500, TimeUnit.MILLISECONDS)
                .ignoring(StaleElementReferenceException.class)
                .ignoring(NoSuchElementException.class)
                .ignoring(ElementNotVisibleException.class)
    }
    

    Use like so:

    WebElement webElement = getFluentWait().until(x -> { return driver.findElement(elementBy); } );
    

    Explicit Wait - Well it's the same as FluentWait but with pre-configured pollingEvery and the type of Wait e.g. FluentWait (faster to use):

    WebDriverWait wait = new WebDriverWait(driver, 30000);
    WebElement item = wait.until(ExpectedConditions.visibilityOfElementLocated(yourBy));
    

    ImplicitWait - Not recommended as it is configured once for all your session. This also is used for every find element and waits for presence only (no ExpectedConditions etc...):

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    

提交回复
热议问题