Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to bottom of div element Selenium Webdriver

I have a use case wherein there's a div element on the webpage, it appears like a popup dialog as soon as you click a link (its not an actual popup, its something like dialog boxes which opens in Facebook when you click a link to check reactions on your posts etc.)

I'm using Selenium WebDriver with Java to automate tests for this application, my use case involves me to scroll to the bottom of the dialog box where there is a link to show more items, when user clicks show more, it populates another 10 items in the list and so on until there are no other items left for the user to visit.

So basically I have to scroll down on that particular div element till I keep seeing Show More link and when driver is not able to find show more link it should stop.

Note - I can't just scroll to bottom of the page using javascript window.scrollTo() as it will scroll down through whole webpage, however I just want to scroll to bottom of that division element only.

If anybody have any idea on how to achieve this please let me know.

Thanks for the help in advance !

like image 947
utkarshs Avatar asked Jul 04 '17 12:07

utkarshs


People also ask

How do you move to the end of an element in selenium?

scroll_Page(scrollArea ,100);

How do I scroll to a div?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do you scroll down a webpage in selenium?

Scenario 1: To scroll down the web page by pixel. Javascript method ScrollBy() scrolls the web page to the specific number of pixels. The syntax of ScrollBy() methods is : executeScript("window. scrollBy(x-pixels,y-pixels)");

How does selenium handle scroll up and down?

The JavaScriptExecutor provides an interface that enables QAs to run JavaScript methods from Selenium scripts. Hence, to scroll up or down with Selenium, a JavaScriptExecutor is a must. The scrollBy() method involves two parameters, x, and y, that represent the horizontal and vertical pixel values, respectively.


1 Answers

Another way of scrolling to the bottom inside (of) any scroll-able element:

public void scrollToBottomInsideDiv(WebElement scrollArea) {
    JavascriptExecutor js = ((JavascriptExecutor) webdriver);
    js.executeScript("arguments[0].scrollTo(0, arguments[0].scrollHeight)", scrollArea);
}
like image 95
Ian Avatar answered Oct 10 '22 23:10

Ian