Test dynamically loaded content with Selenium Web Driver

前端 未结 3 1388
不思量自难忘°
不思量自难忘° 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:08

    I think the problem is waiting for the dynamic content to finish loading. Try to wait 3 seconds just before findElementsBy? In C# the code would be Thread.Sleep(3000);

    0 讨论(0)
  • 2021-01-12 14:20

    I would recommend using WebDriverWait with ExpectedConditons.

    //scroll down with Javascript first
    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("selector")));
    //interact with your element
    element.click()
    

    Take a look at the guidance provided by Selenium Official page: http://seleniumhq.org/docs/04_webdriver_advanced.html

    0 讨论(0)
  • 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<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(30, TimeUnit.SECONDS)
                    .pollingEvery(5, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class);
    
            WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
                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

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