7

The Scroll element in the page is actually a div with scroll bound to it. Here is the HTML snippet.

<div class="slimScrollBar" style="background: none repeat scroll 0% 0% rgb(137, 137, 137); width: 12px; position: absolute; border-radius: 0px; z-index: 99; right: 1px; top: -13px; opacity: 0.6; height: 224.835px; display: block;"/> 

I have tried with the following code, but it didn't work out

WebElement scrollArea = driver.findElement(By.cssSelector("div.slimScrollBar"));
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].scrollTop = arguments[1];",scrollArea, 250); 
Thread.sleep(1000); 

Help is appreciated!

asked Sep 23, 2015 at 10:17
4
  • 2
    This is same as/duplicate of sqa.stackexchange.com/questions/9655/… Commented Sep 23, 2015 at 10:44
  • It doesn't worked for me. The problem is, the scroll bar is only visible when the mouse over is performed in the particular div. So its throwing "unknown error: cannot focus element" Commented Sep 24, 2015 at 5:30
  • Can you give me an example. I think you can overcome this by using a wait, so that until element is focussed, the webdriver will wait. Commented Sep 24, 2015 at 9:36
  • 3
    Does this answer your question? Unable to scroll down to bottom of div with data loading dynamically Commented Feb 20, 2020 at 13:20

4 Answers 4

4

For Scroll down:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");

or, you can do as follows:

jse.executeScript("scroll(0, 250);");

For Scroll up:

jse.executeScript("window.scrollBy(0,-250)", "");

OR,

jse.executeScript("scroll(0, -250);");
answered Sep 29, 2015 at 7:28
1
  • How would you scroll to the exact position of the webelement Commented Apr 28, 2020 at 13:11
3

You could try this.

WebElement e=driver.findElement(By.xpath("enter xpath which element is in end of the div"));
Coordinates cor=((Locatable)e).getCoordinates();
cor.inViewPort();
Thread.sleep(1000);
ECiurleo
2,0431 gold badge16 silver badges45 bronze badges
answered Mar 9, 2016 at 6:53
3
  • 1
    Could you give a bit more background to your answer. Why is this solution useful in this situation? What are the pros/cons of this approach? The more detailed context you can give the more the answer will help others. Commented Mar 9, 2016 at 10:09
  • ??? what's this now? Commented Mar 9, 2016 at 13:07
  • This worked for me. I searched a lot but just this one works. I want to say you are beautiful and amazing. :) Commented Jan 9, 2018 at 5:13
3

Some HTML page have internal (custom) scroll bar. We have to handle with little bit different way.

Javascript is not working here.

Solution :

WebElement scrollArea = driver.findElement(By.cssSelector("div.slimScrollBar"));

Create method scroll_Page as given below.

Call this method as scroll_Page(scrollArea ,100);

Where scrollArea is your dragged(scroll) element and 100 is scroll points.

public static boolean scroll_Page(WebElement webelement, int scrollPoints)
{
 try
 { 
 Actions dragger = new Actions(driver);
 // drag downwards
 int numberOfPixelsToDragTheScrollbarDown = 10;
 for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown)
 {
 dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform();
 }
 Thread.sleep(500);
 return true;
 }
 catch (Exception e)
 {
 e.printStackTrace();
 return false;
 }
}
answered Mar 9, 2016 at 10:19
1

In Python:

driver.execute_script('document.getElementById("viewport").scrollTop += 100')

In Java:

driver.executeScript('document.getElementById("viewport").scrollTop += 100'); 
JAINAM
1,8453 gold badges19 silver badges39 bronze badges
answered Feb 20, 2020 at 8:40
2
  • Or simply driver.execute_script('window.scrollBy(X, Y)') Commented Jul 20, 2020 at 10:48
  • Yes, this works fine until you try to scroll in a certain item that has its own scroll bar. Commented Jul 21, 2020 at 14:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.