JavaScript Execution – Selenium WebDriver

JavaScript is a programming language of HTML which is used to program the behaviour of web pages.

JavaScript Executor:

Selenium WebDriver provides support to inject any JavaScript on a browser. JavaScriptExecutor is an interface which is used to execute JavaScript through Selenium WebDriver. It has two methods “executeScript” & “executeAsyncScript”  to run JavaScript.

Let us consider an example “Scroll Down Operation” using Selenium WebDriver:

Refer below code:

public void scrollPage(WebDriver Driver, int x, int y){
		String jsCommand;
		JavascriptExecutor jsEngine;
		oJSEngine = (JavascriptExecutor) Driver;
		jsCommand = String.format("window.scrollTo(%d, %d)", x,y);

In the above code, first we have defined a string variable “jsCommand” and another variable “jsEngine” of type javaScriptExecutor. jsCommand is initialised with the JavaScript to be executed, here in this example it is “window.scrollTo(<x-cordinate>, <y-cordinate>)” and jsEngine is used to run the command.

x and y coordinates are integer values, and their value is in pixels.

Practical Scenarios: Scroll down through a page after a product search on an ecommerce site:


  1. Open any browser of your choice say “Chrome.”
  2. Navigate to “”.
  3. Search for a product say, “Apple watches” and select the category as “Watches” and click on Search button.
  4. A search operation is performed, and products are displayed on the page.
  5. Now scroll down to each product one by one and fetch its details.

Fetching the list using XPath locator

Refer the code below:

package day12;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class EbayProject {
	WebDriver Driver;
	public void invokeBrowser(String sBrowserType){
		switch (sBrowserType) {
		case "firefox":
			Driver = new FirefoxDriver();
		case "chrome":
		System.setProperty("", "C:\\workspace\\libs\\chromedriver.exe");
			Driver = new ChromeDriver();
		case "ie":
			System.setProperty("", "C:\\workspace\\libs\\IEDriverServer.exe");
			Driver = new InternetExplorerDriver();

			System.out.println("Invalid BrowserType.. Setting default browser as Firefox..");
			Driver = new FirefoxDriver();
		Driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
		Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	public String searchItem(String sProduct, String sCategory) throws InterruptedException{
		Select dropdown = new Select(Driver.findElement("gh-cat")));
		Actions action = new Actions(Driver);
		return Driver.findElement(By.className("listingscnt")).getText();
	public void getAllProducJS(){
		List<WebElement> allProduct = Driver.findElements(By.xpath("//ul[@id='ListViewInner']/li"));
		for(WebElement product : allProduct){
			int X = product.getLocation().x;
			int Y = product.getLocation().y;
			scrollTo(X, Y);
			String sProduct = product.getText();
		//	String[] aProduct = sProduct.split("/n");
	private void scrollTo(int x, int y){
		String jsCommand;
		JavascriptExecutor jsEngine;
		jsEngine = (JavascriptExecutor) Driver;
		jsCommand = String.format("window.scrollTo(%d,%d)", x, y);


This scenario is an extension of a scenario we saw earlier while learning “Working with Dropdown”You can refer to this tutorial for the explanation of first four steps. Let us focus on 5th step here. To scroll down to each product first, fetch the location (i.e. x and y coordinates) of each product (WebElement) one by one and then call scroll down method.

getLocation().x; – this will return x coordinate on the screen.

getLocation().y; – this will return y coordinate on the screen.

Both the above, integer values are then passed as an argument in the window.scrollTo(int x, int y) method and is iterated over a List which contains all the products as a WebElement using enhanced for loop.

scrollTo is the same method that we saw earlier in this article.

By getText() method, we can fetch the details of all the products.

Above example can be considered as a reference to execute any JavaScript scenario, the only thing which changes is jsCommand. Rest the code will remain same.

For any questions, queries or comments feel free to write us at or Happy Learning ?

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: