Wednesday, 28 August 2013

Page Scroll using Selenium WebDriver


Scroll Down:

import org.openqa.selenium.JavascriptExecutor;
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250)"); //y value '250' can be altered

Scroll up:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); //x value '250' can be altered

Scroll bottom of the Page:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
(or)
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

Full scroll to bottom in slow motion:

for (int second = 0;; second++) {
        if(second >=60){
            break;
        }
            ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '400' can be altered
            Thread.sleep(3000);
}
(or)
JavascriptExecutor jse = (JavascriptExecutor)driver;
for (int second = 0;; second++) {
        if(second >=60){
            break;
        }
            jse.executeScript("window.scrollBy(0,800)", ""); //y value '800' can be altered
            Thread.sleep(3000);
}

Scroll automatically to your WebElement:

Point hoverItem =driver.findElement(By.xpath("Value")).getLocation();
((JavascriptExecutor)driver).executeScript("return window.title;");    
Thread.sleep(6000);
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(hoverItem.getY())+");"); 
// Adjust your page view by making changes right over here (hoverItem.getY()-400)
(or)
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("Value')]")));
(or)
WebElement element = driver.findElement(By.xpath("Value"));
Coordinates coordinate = ((Locatable)element).getCoordinates(); 
coordinate.onPage(); 
coordinate.inViewPort();

46 comments:

  1. I want to know the command to scroll the page using SELENIUM IDE. And also what should be the target?

    ReplyDelete
  2. I wasn't able to get "scroll(x, y)" to work due to my special circumstances. However, I'm tempted to say that where it says "scroll" it should really be "window.scroll", and also questioning why you'd fill in a non-zero first argument to make it scroll up, and a non-zero second argument to make it scroll down. In my experience, a positive first argument makes it scroll down, and a positive second argument makes it scroll *right.* Again, though, I wasn't able to use window.scroll() for my purpose, so I couldn't test it for myself.

    It's fine if you want to scroll the whole window, but I have some divs with the CSS "overflow: auto" that I want to move around in. I'm also using Python, not Java, so my code will look a little different from that standpoint too. Nevertheless, I used:

    browser = selenium2lib._current_browser()
    browser.execute_script("document.getElementById('myDesiredElement').scrollTop += delta")

    The delta variable is an integer; positive makes the div scroll down, negative makes it scroll up.

    Since this is one of the highest-ranking Google results for scrolling in Selenium, I thought I should add that tidbit here. ;)

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi all,
    i want to perform scrolling for mobile application..this coding is not working for mobile apps.can anyone please help me how to perform scrolling in mobile application testing..
    thanks

    ReplyDelete
  5. Is 'window.scrollBy' works on Mac (Other OS) as well?

    ReplyDelete
    Replies
    1. it should work on any os since its javascript

      Delete
  6. How to scroll down on a dialog box which is in focus?

    ReplyDelete
  7. How can we scroll in horizontal style ?

    ReplyDelete
  8. Great way of explaining this..!! Thanks Selenium Tutorials

    ReplyDelete
  9. Great solution. worked out for me :)

    ReplyDelete
  10. I want to know if i can generalize the scrollto script. So as not to use the exact numeric value for the X and Y values but a variable that is passed as a parameter.

    ReplyDelete
  11. How can we do infinite scroll action on page.

    Link: http://the-internet.herokuapp.com/infinite_scroll

    ReplyDelete
  12. for (int second = 0;; second++).. Any one please explain this

    ReplyDelete
  13. Hi...

    After scrolling again my page is loading so how can I do the scroll till end of the page.
    Please help me.
    Note: No element is available at end of the page.

    Thanks in advance.

    ReplyDelete