What is the best way to avoid NoSuchElementException in Selenium?

后端 未结 9 710
时光说笑
时光说笑 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:10

    NoSuchElementException occurs, when the locators (i.e. id / xpath/ css selectors) is unable to find the web element on the web page.

    The reasons for this could be :

    1. Incorrect Locator

    2. Web element not available on web page

      In order to avoid this exception, we can use Fluent Wait. This wait allows us to define max timeout, polling frequency and define which exception to ignore.

    Please find the sample usage of Fluent wait below :

    .withTimeout(50, TimeUnit.SECONDS)
    .pollingevery(3, TimeUnit.SECONDS)
    .ignoring(NoSuchElementException.class);
    

提交回复
热议问题