0

I am trying to scroll down page and get the last element of that section/div. i have executed the code:


Coordinates coordinate = ((Locatable)element).getCoordinates();
    coordinate.inViewPort();

and also tried with

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250)");

But it is scrolling the entire page instead of scrolling the specific section enter image description here

And also the page has pagination

liki
  • 33
  • 3
  • 13
  • Can you fire up seleniumIDE and see what actiosn are generated? It would save time - you can then export in your required format – farrellmr Aug 17 '16 at 08:06
  • In IDE nothing is getting recorded if performing action of scroll down, because i think if any element will be clickable only that actions will be recorded, but here nothing clickable only i have to scroll down – liki Aug 17 '16 at 08:16
  • You will have to use javascript for this. Get the element using document.getel... and use the scrollTop and pass the pixels to it. var elmnt = document.getElementById("myDIV"); elmnt.scrollTop = 100; – Grasshopper Aug 17 '16 at 08:25
  • i have tried this, but this is showing compilation error, for var and document – liki Aug 17 '16 at 08:35

2 Answers2

2

First you need to find the element you're trying to scroll to:

WebElement element = driver.findElement(By.xpath("xpath_here")); //or anything else used to identify the element

Afterwards, you can execute JS using JavascriptExecutor to bring the element into view using scrollIntoView() :

((JavaScriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
Cosmin
  • 2,354
  • 2
  • 22
  • 39
0

What you have tried will scroll the window scroll bar, try something like below

JavascriptExecutor je = (JavascriptExecutor) dr;  
je.executeScript("arguments[0].scrollIntoView(true);",dr.findElement(By...));

Specify the locator of the element which you are trying to find by scrolling down.

arguments[0]

Edit:

JavascriptExecutor je = (JavascriptExecutor) dr;  
je.executeScript("arguments[0].scrollTop(arguments[0].scrollHeight);",dr.findElement(By...));
Siva
  • 1,129
  • 1
  • 8
  • 13
  • i just want to scroll down the page till the last element comes because last element is not a constant it will be changed every time and also numbers will increase, and if am trying this its not getting the element as pagination is also there in the section. – liki Aug 17 '16 at 08:30
  • Try this http://stackoverflow.com/questions/270612/scroll-to-bottom-of-div – Siva Aug 17 '16 at 08:37
  • var objDiv = document.getElementById("your_div"); objDiv.scrollTop = objDiv.scrollHeight; here i have to mention the id of div how can i get it – liki Aug 22 '16 at 10:21
  • Not necessiraly the div id, it is the elemnet inside which your scroll bar is present. It can be span, table, frame what ever it is. – Siva Aug 22 '16 at 11:10