What is the best way to avoid NoSuchElementException in Selenium?

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

    We can apply below codes to remove this exception condition

    1. By applying WebDriverWait, webdriver object wait for a specific time (in second) of an element for its visibility.

            WebDriverWait wait = new WebDriverWait(driver, 10);       
             wait.until(ExpectedConditions.visibilityOf(link));
      
    2. We can handle NoSuchElementException through try-catch block inside Generic method

       public boolean isElementPresent(By by) {
       boolean isPresent = true;
       try {
       driver.findElement(by);
       } catch (NoSuchElementException e) {
        isPresent = false;
       }
      return isPresent
      }
      

    http://selenium-code.blogspot.in/2017/08/selenium-exception-nosuchelementexcepti.html

提交回复
热议问题