What is the best way to avoid NoSuchElementException in Selenium?

后端 未结 9 719
时光说笑
时光说笑 2020-11-27 18:31

I have written few test cases in Selenium WebDriver using Java and execute them on grid (hub and multiple nodes). I have noticed that a few test cases fail due to NoSu

9条回答
  •  执念已碎
    2020-11-27 19:08

    you can also use FluentWait,

    Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.

    Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

    // Waiting 30 seconds for an element to be present on the page, checking
       // for its presence once every 5 seconds.
       Wait wait = new FluentWait(driver)
           .withTimeout(30, SECONDS)
           .pollingEvery(5, SECONDS)
           .ignoring(NoSuchElementException.class);
    
       WebElement foo = wait.until(new Function() {
         public WebElement apply(WebDriver driver) {
           return driver.findElement(By.id("foo"));
         }
       });
    

    Click here for more info

提交回复
热议问题