12

I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.

So when I click on certain elements that are present below the current visible region of the browser, selenium tries to scroll the element into view and click them.

But because of the auto scrolling as such the elements are scrolled behind the floating header and when any action is performed on them, the elements in the page header get clicked.

is there any way to limit the default scroll of the WebDriver?

T J
  • 42,762
  • 13
  • 83
  • 138
Rogers Jefrey L
  • 256
  • 2
  • 5
  • 15
  • I understand that you would like to control the scroll using Selenium WebDriver. There is a similar issue discussed/solved [here](http://sqa.stackexchange.com/a/1292/1316), which I hope is useful. – self-babush Feb 26 '12 at 12:48
  • Thanks , I wrote a javascript to suite my needs – Rogers Jefrey L Mar 11 '12 at 03:20
  • You can get the solution here: http://stackoverflow.com/questions/12293158/page-scroll-up-or-down-in-webdriver-selenium-2-using-java OR, http://stackoverflow.com/questions/11554370/vertical-scroll-down-and-scroll-up-in-webdriver-with-java – Ripon Al Wasim Apr 04 '14 at 05:21
  • What programming language are you using? – Ripon Al Wasim Oct 31 '14 at 04:23

8 Answers8

6
    Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
    int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
TomaszB
  • 69
  • 1
4

If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:

JavascriptExecutor js = (JavascriptExecutor)driver;
                    js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
                    "document.body.scrollHeight,document.documentElement.clientHeight));");
Ankit Pandey
  • 339
  • 3
  • 4
  • Perfect for me. Scrolls straight to the bottom :) - for those that want a JS/protractor version `browser.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");` – rob_james Mar 13 '14 at 12:00
2

You can scroll to the necessary location using javascript You need to use the scrollTo method rather than the scrollBy method for it to work.

public void scrollToElement(By by) {
    Locatable element = (Locatable) selenium.findElement(by);
    Point p= element.getCoordinates().getLocationOnScreen();
    JavascriptExecutor js = (JavascriptExecutor) selenium;  
    js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()+150) + ");");
}
Aaron Levenstein
  • 385
  • 2
  • 12
2

Simple use the .sendKeys(Keys.PAGE_DOWN); when your element was visible, just click on it, by .click(element).perform(); for me work something like this:

clicker = new Actions(driver);
    clicker.sendKeys(Keys.PAGE_DOWN);
    Thread.sleep(1000);
    clicker.click(button).perform();     
    Thread.sleep(1000);
T J
  • 42,762
  • 13
  • 83
  • 138
Artemy
  • 21
  • 1
2

Scroll to top can be done:

private void scrollToTop() {
    JavascriptExecutor js = (JavascriptExecutor) webDriver;
    js.executeScript("window.scrollTo(0, 0);");
}
Jonathan L
  • 9,552
  • 4
  • 49
  • 38
2

For scrolling down:

System.setProperty("webdriver.chrome.driver",
                   "/home/shreetesh/chromedriver");
WebDriver driver = new ChromeDriver(); 
String url = "https://en.wikipedia.org/wiki/Main_Page";
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0, 25000);");

To scrolling up just replace the value of scroll with (2500, 0).

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
1

Use below code for scrolling up and scrolling down

Actions dragger = new Actions(driver);

WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("<Scroll bar Element >"));

// drag downwards

int numberOfPixelsToDragTheScrollbarDown = 50;

for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
    try {
        // this causes a gradual drag of the scroll bar, 10 units at a time
        dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
        Thread.sleep(1000L);
    } catch(Exception e1){}
} 

// now drag opposite way (downwards)
numberOfPixelsToDragTheScrollbarDown = -50;
for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
    // this causes a gradual drag of the scroll bar, -10 units at a time
    dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
    Thread.sleep(1000L);
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Maddy
  • 63
  • 7
0

I recently had this problem due to a Drupal menu blocking the element when I ran this code:

public void scrollTo(WebElement x) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", x);
        }

After referencing this page, I updated to set the boolean to false using this code, and it works great:

public void scrollTo(WebElement x) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", x);
        }
bjones01001101
  • 1,071
  • 1
  • 12
  • 21