Pages

Wednesday 20 March 2013

Deal with Multiple Scroller inside a Webpage using Selenium

In our Automation journey, we may come across situations where we need to scroll a specific webpage or web element to traverse through contents present inside. In Selenium, we don't have any Pre-defined/built-in method for that, but we can introduce JavaScript methods to satisfy our automation needs. Please go through the examples written below to get an idea on how to do that -

Deal with Scroller present within a Web Page :


WebDriver webDriver = new FirefoxDriver();
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver();
eventFiringWebDriver.executeScript("scroll(<pixel-to-scroll-in-horizontal>,<pixel-to-scroll-in-vertical>)");

Note - <pixel-to-scroll-in-horizontal> and <pixel-to-scroll-in-vertical> should be the number of pixel we want to scroll through in horizontal and vertical respectively. 
For example, if we want to scroll through 100 pixels in horizontal and 200 pixels in vertical, we need to declare :

eventFiringWebDriver.executeScript("scroll(100,200)");



Deal with Scroller present within a Web Element :


WebDriver webDriver = new FirefoxDriver();
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver();
eventFiringWebDriver.executeScript("document.getElementById('<id-of-the-webelement>').scrollTop = <pixel-to-scroll>");

Note - <pixel-to-scroll> should be the number of pixel we want to scroll through. 
For example, if we want to scroll through 200 pixels, we need to declare :

eventFiringWebDriver.executeScript("document.getElementById('<id-of-the-webelement>').scrollTop = 200");


4 comments:

  1. Thanks for the suggestion you gave for scrolling a specific DIV within a page.
    I was searching for a solution for the same but maximum cases I ended up finding scrolling the complete page :(
    Thanks a lot again :)

    ReplyDelete
  2. Thank you very much..it worked seamlessly.

    ReplyDelete
  3. Thanks a lot...it worked seamlessly

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like your blog.
    selenium Online Training Hyderabad

    ReplyDelete