What is the best way to avoid NoSuchElementException in Selenium?

You can never be sure that element will be found, actually this is purpose of functional tests – to tell you if anything changed on your page. But one thing which definitely helps is to add waits for the elements which are often causing NoSuchElementException like

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Leave a Comment