I have written the following code in Selenium 1 (a.k.a Selenium RC) for page scrolling using java:
selenium.getEval("scrollBy(0, 250)");
What is the equivalent code in Selenium 2 (WebDriver)?
I have written the following code in Selenium 1 (a.k.a Selenium RC) for page scrolling using java:
selenium.getEval("scrollBy(0, 250)");
What is the equivalent code in Selenium 2 (WebDriver)?
Scrolling to Bottom of a page:
This code works for me. As the page which I'm testing, loads as we scroll down.
I did not want to use JavaScript, or any external libraries, so this was my solution (C#):
You can also easily make this an extension method for scrolling up or down on any element.
Thanks for Ripon Al Wasim's answer. I did some improvement. because of network problems, I retry three times until break loop.
Javascript executor always does the job perfectly:
where
(0,300)
are the horizontal and vertical distances respectively. Put your distances as per your requirements.If you a perfectionist and like to get the exact distance you like to scroll up to on the first attempt, use this tool, MeasureIt. It's a brilliant firefox add-on.
If you want to scroll the page vertically to perform some action, you can do it using the following JavaScript. ((JavascriptExecutor)driver).executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
import org.openqa.selenium.JavascriptExecutor;
2.If you want to scroll at a particular element, you need to use the following JavaScript.
WebElement element = driver.findElement(By.xpath(“//input [@id=’email’]”));((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView();”, element);
Where ‘element’ is the locator where you want to scroll.
3.If you want to scroll at a particular coordinate, use the following JavaScript.
((JavascriptExecutor)driver).executeScript(“window.scrollBy(200,300)”); Where ‘200,300’ are the coordinates.
4.If you want to scroll up in a vertical direction, you can use the following JavaScript. ((JavascriptExecutor) driver).executeScript(“window.scrollTo(document.body.scrollHeight,0)”);
If you want to scroll horizontally in the right direction, use the following JavaScript. ((JavascriptExecutor)driver).executeScript(“window.scrollBy(2000,0)”);
If you want to scroll horizontally in the left direction, use the following JavaScript. ((JavascriptExecutor)driver).executeScript(“window.scrollBy(-2000,0)”);