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>));

Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

NoSuchElementException selenium.common.exceptions.NoSuchElementException popularly known as NoSuchElementException is defined as : exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None) NoSuchElementException is basically thrown in 2 cases as follows : When using : webdriver.find_element_by_*(“expression”) //example : my_element = driver.find_element_by_xpath(“xpath_expression”) When using : element.find_element_by_*(“expression”) //example : my_element = element.find_element_by_*(“expression”) As per the API Docs just like any other selenium.common.exceptions, NoSuchElementException should contain … Read more