Skip to content

Javascript executor in selenium Java: How to use it?

Javascript executor in selenium Java: How to use it?

The Javascript executor in Selenium Java allows you to execute Javascript code in your WebDriver tests. This can be useful for interacting with web pages that have dynamic content, or for performing certain actions that are not supported by the standard Selenium API.

Selenium WebDriver

In Selenium WebDriver, the JavaScript executor provides several methods to execute JavaScript code within the context of the browser. Here are some commonly used methods of the JavaScript executor:

  1. executeScript(String script, Object... args): This method executes the provided JavaScript code with optional arguments. It returns the result of the execution. Example:
   JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
   String title = (String) jsExecutor.executeScript("return document.title;");
  1. executeAsyncScript(String script, Object... args): This method is similar to executeScript(), but it executes the JavaScript code asynchronously. It is useful for handling scenarios where the execution might take longer. Example:
   JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
   Long loadTime = (Long) jsExecutor.executeAsyncScript(
       "var callback = arguments[arguments.length - 1];" +
       "setTimeout(function() { callback(performance.timing.loadEventEnd); }, 5000);"
   );
  1. executeAsyncScript(String script, Map<String, ?> args): This overloaded version of executeAsyncScript() allows you to pass a map of arguments to the JavaScript code. Example:
   JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
   Map<String, Object> args = new HashMap<>();
   args.put("username", "john");
   args.put("password", "secretpassword");
   jsExecutor.executeAsyncScript("login(arguments[0], arguments[1]);", args);

These are just a few examples of the JavaScript executor methods available in Selenium WebDriver. You can use these methods to perform various actions, interact with elements, retrieve data, modify styles, and more by executing JavaScript code within the browser context.

Below are some examples of how to use the Javascript executor in Selenium Java:

Execute some Javascript code or pass arguments to Javascript code

// First, create an instance of the Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;

// Then, use the "executeScript" method to execute some Javascript code
js.executeScript("alert('Hello, world!');");

// You can also pass arguments to your Javascript code using the "executeScript" method
String name = "John";
js.executeScript("alert('Hello, " + name + "!');");

In this example, we first create an instance of the Javascript executor by casting our WebDriver object to a JavascriptExecutor object. We then use the executeScript method to execute some Javascript code, which in this case is simply an alert box with the message “Hello, world!”.

We can also pass arguments to our Javascript code by concatenating them into the script string. In the second example, we pass in a name variable and use string concatenation to display a personalized alert message.

Clicking a hidden element

// First, find the hidden element using the standard Selenium API
WebElement element = driver.findElement(By.id("my-hidden-element"));

// Then, use the Javascript executor to click the element
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

In this example, we first locate a hidden element using the standard Selenium API. Then, we use the Javascript executor to click the element by calling the click() method on the element.

Scrolling to the bottom of the page

// Use the Javascript executor to scroll to the bottom of the page
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight);");

In this example, we use the window.scrollTo method to scroll to the bottom of the page. The scrollTo method takes two arguments: the x-coordinate and the y-coordinate of the position to scroll to. In this case, we set the x-coordinate to 0 and the y-coordinate to the height of the document body, which effectively scrolls to the bottom of the page.

Setting the value of a hidden input field

// First, find the hidden input field using the standard Selenium API
WebElement element = driver.findElement(By.id("my-hidden-input"));

// Then, use the Javascript executor to set the value of the input field
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value = 'new value';", element);

In this example, we first locate a hidden input field using the standard Selenium API. Then, we use the Javascript executor to set the value of the input field by assigning a new value to the value property of the element.

Scroll to and bring element into view

// Finally, you can also use the Javascript executor to interact with elements on the page
WebElement element = driver.findElement(By.id("my-element"));
js.executeScript("arguments[0].scrollIntoView();", element);

Finally, we can use the Javascript executor to interact with elements on the page. In the last example, we first locate an element using the standard Selenium API (driver.findElement(By.id("my-element"))), and then use the Javascript executor to scroll the element into view using the scrollIntoView method.

Hope this article helps you to have a basic understanding about Javascript executor in Selenium. 🙂

Please share