Monday 5 May 2014

Scrolling the Webpage in Selenium Webdriver


There are 3 ways to scroll a webpage using Selenium Webdriver.
·         Using JavascriptExecutor
·         Using Actions class.
·         Using simple sendKeys() function.

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

Using Actions class.

               import org.openqa.selenium.interactions.Actions
 Actions action=new Actions(driver);
 action.sendKeys(Keys.DOWN).perform();
 action.sendKeys(Keys.DOWN).perform();
 action.sendKeys(Keys.DOWN).perform();
 action.sendKeys(Keys.DOWN).perform();

Using simple sendKeys() function.

for ( int i=0 ; i<10 ; i++ ) {
                             driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN);
                    }