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!

4

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);");
1
  • How would you scroll to the exact position of the webelement 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);
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.
    – ECiurleo
    Mar 9, 2016 at 10:09
  • ??? what's this now? 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. :)
    – Evan Hu
    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;
    }
}
1

In Python:

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

In Java:

driver.executeScript('document.getElementById("viewport").scrollTop += 100'); 
2
  • Or simply driver.execute_script('window.scrollBy(X, Y)')
    – Mr.Coffee
    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. Jul 21, 2020 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.