5

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

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);");
| improve this answer | |
  • How would you scroll to the exact position of the webelement – Ashok kumar Ganesan Apr 28 at 13:11
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;
    }
}
| improve this answer | |
2

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);
| improve this answer | |
  • 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 '16 at 10:09
  • ??? what's this now? – IAmMilinPatel Mar 9 '16 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 '18 at 5:13
0

In Python:

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

In Java:

driver.executeScript('document.getElementById("viewport").scrollTop += 100'); 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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