简体   繁体   中英

How to move to bottom of the dynamic page using Java + Selenium WebDriver

I am trying to get the number of rows in a page that is completely dynamic. I have the below code to do so. But it is scrolling to a certain extent and fetching only a partial count of the elements. Cannot locate any element at the bottom of the page as it is a dynamic one. How can I resolve this issue?

List<WebElement> elements = driver.findElements(By.xpath(NoOfAssets));
Actions a = new Actions(driver);
a.moveToElement(elements.get(elements.size() - 1)).clickAndHold().moveByOffset(0, 1000000000).release().perform();
int noOfAssets = elements.size();
System.out.println(noOfAssets);

It is returning 40 rows only. But there are more rows actually. I want to drag the cursor till the bottom of the page as and when the rows get loaded and then get the size.

You need to scroll down to the end of the page then find the elements .However You can use sendKeys method to Scroll down pages.Try following code.Let me know how it goes.


JavascriptExecutor js = (JavascriptExecutor)driver;

        while(true){

            Long height=(Long) js.executeScript("return document.body.scrollHeight");
            System.out.println(height);
            Thread.sleep(1000);
            driver.findElement(By.tagName("body")).sendKeys(Keys.END);        

            if (height==0)
            {
                break;
            }               

        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM