0

In my page I have lots of data. The first time page opened i can see only number of data. When I scroll down the page other datas are starting to appear. It is going repeatedly. Problem is I wanna see all datas in my page and take their names but when I use List <WebElement> listItems=driver.findElement(AAA).findElements(BBB);this code, I can only get datas from first page. How can I see the others? (I don't want to use an id or class at the bottom. And scroll till to see it)

ABC
  • 7
  • 1
  • 4
  • I think this might actually be a duplicate of the post I attached as a link which goes into some detail on a solution for scrolling so that the elements actually appear. [Click here for the question](https://stackoverflow.com/questions/26729532/how-to-search-for-element-in-dynamic-loading-grid-on-scroll-using-selenium-webdr) – Wunderbread Jun 05 '17 at 02:07
  • But that answer is looking for a specific element. I want to see all elements and scroll down till there is no one left. @Wunderbread – ABC Jun 05 '17 at 02:16
  • Ah, that's true. If I was designing the test myself I would bake automation into the page and set an ID at the bottom and set selenium to select that ID so that the page is expanded showing all elements. I went ahead and voted on an answer that I feel would help you within this post. – Wunderbread Jun 05 '17 at 16:27

2 Answers2

2

Well, you surely can scroll to the bottom. The simplest way is to use JavaScript to see the scroll height and scroll to it:

JavascriptExecutor jse = (JavascriptExecutor) driver;
Long scrollHeight = (Long) jse.executeScript("return document.scrollHeight;");
jse.ExecuteScript("document.scrollTop="+scrollHeight);

However, this only works if the page already has a scroll bar with the correct height. If it does not render fully at once, the scroll height might not be correct. There is a way around this, but I have never tried this with pages, only tables.

You press the PgDn button and check the scrollTop. You need to wait a second after each PgDn, because rendering the scrolling might take some time and you need the scrollTop to be correct already. As soon as the previous and current scrollTop are the same, you are done.

I have not personally tried the method I use here to press PgDn. I just send it to the body element here. The alternative is to use Actions.

JavascriptExecutor jse = (JavascriptExecutor) driver;
WebElement body = driver.findElement(By.tagName("body"));
String prevTop ="-1";
boolean continueScrolling = false;
do {
    body.sendKeys(Keys.PAGE_DOWN);
    Thread.sleep(1000); // you might need to add handling for InterruptedException
    String currentTop = String.valueOf(jse.executeScript("return document.scrollTop;"));
    continueScrolling = !currentTop.equals(prevTop);
    prevTop = currentTop;
} while (continueScrolling);
Mikhail Ramendik
  • 1,063
  • 2
  • 12
  • 26
0

You can execute a javascript code like this:

WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

//do anything you want with the element
graph
  • 77
  • 4