How to Process Java Script Executor in Selenium Test Automation | Devstringx Technologies

How to get started with Query Scripts?

With the help of JavaScript Executor Interface we can execute query scripts directly in selenium .It provides two methods such as “executeScript” & “executeAsyncScript” to run JavaScript code.To make the availability of these methods we need to downcast JavaScript Executor Interface with Web Driver reference.

Some of the query methods are mentioned below -

  • querySelector() method in HTML DOM is used to find the first HTML Element.This method function is similar to findElement() in selenium.

Syntax: document.querySelector(selector)

  • querySelectorAll() method in HTML DOM is used to return the list of HTML Elements.It can be accessed by indexing starts at 0.This method function is similar to findElements() in selenium

Syntax: document.querySelectorAll(selector)

‘document’ is an object of Javascript Document that provides access to all HTML elements of a document. ‘selector’ represents a string containing one or more css selectors.

Importing the package

import org.openqa.selenium.JavascriptExecutor;

Creating a reference

JavascriptExecutor js = (JavascriptExecutor) driver;

Creating query script

script =document.querySelector(selector)

Calling the method



  • click : script =document.querySelector(selector).click();
  • send text :script =document.querySelector(selector).value=’data’;
  • get text : script =document.querySelector(selector).textContent;
  • get attribute : script =document.querySelector(selector).getAttribute(“class”);
  • get height : script =document.querySelector(selector).offsetHeight;
  • get width : script =document.querySelector(selector).offsetWidth;
  • get style: script =document.querySelector(selector).style;

Let’s create one simple ’QueryScriptsTest‘ automation script using Selenium that searches Amazon from google by JavascriptExecutor method.

package tests;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import;import org.openqa.selenium.remote.RemoteWebDriver;
public class QueryScriptsTest {
public static void main(String[] args) {System.setProperty(“”, “./drivers/chromedriver.exe”);WebDriver driver=new ChromeDriver();driver.manage().window().maximize();driver.get(“");JavascriptExecutor js = (JavascriptExecutor)driver;//send textString sendAmazonText=”document.querySelector(\”input[title=’Search’]\”).value=\”Amazon\”;”;js.executeScript(sendAmazonText);String clickSearchBtn =”document.querySelectorAll(\”input[value=’Google Search’]\”)[1].click()”;//click buttonjs.executeScript(clickSearchBtn);}}

The above program opens the chrome browser and it navigates to It executes query scripts ‘sendAmazonText’ and ‘clickSearchBtn’ through JavaScript Executor.

Originally published at on Oct 22, 2021.




Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Write Outside Click on React

Crypto Curry with Elixir

React-Starter-Kit: testing isomorphic-style-loader with jest and enzyme

Angular HTTP request auto cancellation decorator

How to Build a React Website Powered by the Cosmic JS GraphQL API

Understanding ‘this’ keyword in javascript

Read configuration properties from JSON file and load it before Angular application is initialized

React-Native Application — Using Hooks For Firebase Authentication And Cloud Firestore

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Devstringx Technologies

Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

More from Medium

How to handle Mouse Actions In Selenium?

Performing Page Interactions with Selenium Actions - TestProject

JSON Schema Validation using Rest Assured

Selenium vs. OATS vs. Opkey: How To Choose Right Oracle Test Automation Solution

How does Selenium Page Factory work with Cucumber?