What is the best way to avoid NoSuchElementException in Selenium?

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

    public WebElement fluientWaitforElement(WebElement element, int timoutSec, int pollingSec) {
    
        FluentWait fWait = new FluentWait(driver).withTimeout(timoutSec, TimeUnit.SECONDS)
        .pollingEvery(pollingSec, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class, TimeoutException.class);
    
        for (int i = 0; i < 2; i++) {
            try {
                //fWait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='reportmanager-wrapper']/div[1]/div[2]/ul/li/span[3]/i[@data-original--title='We are processing through trillions of data events, this insight may take more than 15 minutes to complete.']")));
                fWait.until(ExpectedConditions.visibilityOf(element));
                fWait.until(ExpectedConditions.elementToBeClickable(element));
            } 
            catch (Exception e) {
    
                System.out.println("Element Not found trying again - " + element.toString().substring(70));
                e.printStackTrace();
            }
        }
    
        return element;
    }
    

提交回复
热议问题