How to wait until an element is present in Selenium?

后端 未结 5 1582
情话喂你
情话喂你 2020-11-27 02:53

I\'m trying to make Selenium wait for an element that is dynamically added to the DOM after page load. Tried this:

fluentWait.until(ExpectedConditions.presen         


        
相关标签:
5条回答
  • 2020-11-27 03:40

    Let me recommend you using Selenide library. It allows writing much more concise and readable tests. It can wait for presence of elements with much shorter syntax:

    $("#elementId").shouldBe(visible);
    

    Here is a sample project for testing Google search: https://github.com/selenide-examples/google

    0 讨论(0)
  • 2020-11-27 03:43
    public WebElement fluientWaitforElement(WebElement element, int timoutSec, int pollingSec) {
    
        FluentWait<WebDriver> fWait = new FluentWait<WebDriver>(driver).withTimeout(timoutSec, TimeUnit.SECONDS)
            .pollingEvery(pollingSec, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class, TimeoutException.class).ignoring(StaleElementReferenceException.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;
    
        }
    
    0 讨论(0)
  • 2020-11-27 03:48
    WebDriverWait wait = new WebDriverWait(driver,5)
    wait.until(ExpectedConditions.visibilityOf(element));
    

    you can use this as some time before loading whole page code gets executed and throws and error. time is in second

    0 讨论(0)
  • 2020-11-27 03:50

    FluentWait throws a NoSuchElementException is case of the confusion

    org.openqa.selenium.NoSuchElementException;     
    

    with

    java.util.NoSuchElementException
    

    in

    .ignoring(NoSuchElementException.class)
    
    0 讨论(0)
  • 2020-11-27 03:54

    You need to call ignoring with exception to ignore while the WebDriver will wait.

    FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(200, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);
    

    See the documentation of FluentWait for more info. But beware that this condition is already implemented in ExpectedConditions so you should use

    WebElement element = (new WebDriverWait(driver, 10))
       .until(ExpectedConditions.elementToBeClickable(By.id("someid")));
    

    *Update for newer versions of Selenium:

    withTimeout(long, TimeUnit) has become withTimeout(Duration)
    pollingEvery(long, TimeUnit) has become pollingEvery(Duration)
    

    So the code will look as such:

    FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
            .withTimeout(Duration.ofSeconds(30)
            .pollingEvery(Duration.ofMillis(200)
            .ignoring(NoSuchElementException.class);
    

    Basic tutorial for waiting can be found here.

    0 讨论(0)
提交回复
热议问题