Previous tutorial we have learned popup window handling and now we will learn Scroll Up or Down in Selenium
What is Scrolling?
Scrolling is nothing but moving the screen in horizontal and vertical direction.
How to Scroll Up or Down in Selenium?
If a particular element is not visible in current web application window because of which we need scrolling to view the element in a particular direction .
Scrolling in web page is performed in two ways horizontal scrolling and vertical scrolling.Lets discuss both of them in details.
What is Vertical Scrolling ?
1) Vertical Scrolling: It can be performed when we try to scroll the web page in downward direction with its pixels.The syntax for vertical scrolling is as below:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
Lets consider a scenario to perform vertical scrolling:
- Open a web application www.code2test.com
- Click to selenium menu.
- Now perform the vertical scrolling.
package SeleniumArchitecture;
import java.util.Base64;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
public class Scrolling_using_selenium {
public static void main(String[] args) throws InterruptedException {// System property of the Chrome driver
System.setProperty( "webdriver.chrome.driver","C:\\Users\\XXXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");
// Create oject of chrome driver WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
ChromeDriver driver= new ChromeDriver();
driver.manage().window().maximize(); Thread.sleep(3000);
driver.get("https://code2test.com/");
//click to selenium menu
driver.findElement(By.xpath("//*[@id='menu-post-1']/li[3]/a")).click();
Thread.sleep(3000);
JavascriptExecutor js = (JavascriptExecutor) driver;
//Scroll vertical direction 100 pixel
js.executeScript("window.scrollBy(0,1000)");
}
}
ScrollIntoView():This code will scroll in the web page until the particular element gets visible to the current page. It keeps on scrolling in the page till the visibility on the screen. The Syntax of ScrollIntoView() is below:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();",Element );
Lets consider a scenario to perform vertical scrolling:
- Open a web application www.code2test.com
- Click to selenium menu.
- Now perform the vertical scrolling and .
package SeleniumArchitecture;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Scrolling_using_selenium {
public static void main(String[] args) throws InterruptedException {// System property of the Chrome driver
System.setProperty( "webdriver.chrome.driver","C:\\Users\\XXXXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");
// Create oject of chrome driver WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
ChromeDriver driver= new ChromeDriver();
driver.manage().window().maximize(); Thread.sleep(3000);
driver.get("https://code2test.com/");
//click to selenium menu
driver.findElement(By.xpath("//*[@id='menu-post-1']/li[3]/a")).click();
Thread.sleep(3000);
//Xpath from select From Date
WebElement selectFromDateTitle= driver.findElement(By.xpath("//*[@id='post-30']/div/div[8]//li[3]/a"));
JavascriptExecutor js = (JavascriptExecutor) driver;
//Scroll vertical direction 100 pixel
js.executeScript("arguments[0].scrollIntoView();", selectFromDateTitle);
}
}
In the above code “selectFromDateTitle” is the locator of an element.
“argument[0]” is the index of page which stars from “0”
What is Horizontal Scrolling?
2) Horizontal scrolling: In horizontal scrolling the scrolling is performed on left to right Or right to left on web page, the horizontal scrolling can be done by coding by providing the x-axis co-ordinates on ScrollBy( ) method. Let see the syntax below.
executeScript(“window.scrollBy(x-pixels,y-pixels)”);
Also horizontal scrolling can be performed by ScrollIntoView() method, where an scrolling is performed till the element is available on the current page.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();",Element );
On next topic we will cover integration part in selenium webdriver.