Home > Back-end >  How to count webElements on dynamic page? java , selenium
How to count webElements on dynamic page? java , selenium

Time:08-18

I need to count webElements on page. Initially, it opens only few elements, but when you scroll down - new elements appear. You can't use ExpectedConditions.numberOfElementsToBeLessThan() because it's not known how many elements will be on page.

To solve the problem i need just make selenium wait for few seconds until new elements appear in DOM. But it's not allowed to use thread.sleep. So, how can i fix that?

CodePudding user response:

Find some elements on the page that are loaded last or changing their status indicating the page content now is fully loaded and use ExpectedConditions to wait for those conditions.

CodePudding user response:

First Use below code to keep on Scrolling till end of page , then find and count the total

        By by = "your By";
        WebDriver webDriver = "driver isntance";
        Wait wait = new 
        FluentWait(webDriver).withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofMillis(250));
        long lastHeight = (long) ((JavascriptExecutor) webDriver)
                .executeScript("return document.body.scrollHeight");
        while (true) {
            int currentcount = webDriver.findElements(by).size();
            ((JavascriptExecutor) webDriver)
                    .executeScript("window.scrollTo(0, document.body.scrollHeight);");
            try {
                wait.until(waitForCountToBeHigherThan(by, currentcount));
            } catch (TimeoutException e) {
                //Ignoring
            }
            long newHeight = (long) ((JavascriptExecutor) webDriver)
                    .executeScript("return document.body.scrollHeight");
            if (newHeight == lastHeight) {
                //No Height Change Breaking
                break;
            }
            lastHeight = newHeight;
        }
        List<WebElement> elements = webDriver.findElements(by);
        System.out.println(elements.size());

Define an custom expected condition like below to wait for elements count to be large than current

public static ExpectedCondition<List<WebElement>> waitForCountToBeHigherThan(
            final By locator, final int elementsCount) {
        return new ExpectedCondition<List<WebElement>>() {
            @Override
            public List<WebElement> apply(WebDriver driver) {
                List<WebElement> elements = driver.findElements(locator);
                if (elements.size() < elementsCount) {
                    return null;
                }
                for (WebElement element : elements) {
                    if (!element.isDisplayed()) {
                        return null;
                    }
                }
                return elements;
            } 
        };
    }
  • Related