Test dynamically loaded content with Selenium Web Driver

前端 未结 3 1396
不思量自难忘°
不思量自难忘° 2021-01-12 13:41

I am working on a system that has a web based frontend that I am testing with Selenium. On one page the content is dynamically loaded when scrolling down (maybe you know tha

3条回答
  •  庸人自扰
    2021-01-12 14:22

    try using fluent wait in particular. The main feature is:

    An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

    public WebElement fluentWait(final By locator){
            Wait wait = new FluentWait(driver)
                    .withTimeout(30, TimeUnit.SECONDS)
                    .pollingEvery(5, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class);
    
            WebElement foo = wait.until(
    new Function() {
                public WebElement apply(WebDriver driver) {
                            return driver.findElement(locator);
                    }
                    }
    );
                               return  foo;              }     ;
    

    The method described returns you web element you can operate with. So the approach be the following: 1) you need to find the selectors of elements you expect to be rendered after scrolling e.g.

    String cssSelector = "blablabla"
    

    2) scroll down with js 3)

    WebElement neededElement  = fluentWait(cssSelector);
    neededElement.click();
    //neededElement.getText().trim();
    

    you can get more info about fluent wait here

提交回复
热议问题