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

Back to Blog
Selenium Test Automation

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

Methods of Selenium Query Selector

  • 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 access by indexing starting 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.

How to Get Started With Query Scripts?

With the help of the 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 the JavaScript Executor Interface with Web Driver reference.

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


Read Also:- Write Unit Test Case on Slim 3 to Test API

Query Scripts

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;

Create one simple ’QueryScriptsTest‘automation script using Selenium that searches Amazon from google by the JavascriptExecutor method.

package tests;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;


import org.openqa.selenium.remote.RemoteWebDriver;

public class QueryScriptsTest {

public static void main(String[] args) {

System.setProperty("", "./drivers/chromedriver.exe");

WebDriver driver=new ChromeDriver();



JavascriptExecutor js = (JavascriptExecutor)driver;

//send text

String sendAmazonText="document.querySelector(\"input[title='Search']\").value=\"Amazon\";";


String clickSearchBtn ="document.querySelectorAll(\"input[value='Google Search']\")[1].click()";

//click button




Read Also:- What is ETL Testing: Importance, Process, and Types


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

  • How to Use JavaScriptExecutor in Selenium?
  1. Bring the parcel in. the org. open a.selenium import. Make a reference using the JavascriptExecutor. driver = (JavascriptExecutor) JavascriptExecutor;
  2. A JavascriptExecutor method call is made. Javascript: js.executeScript(script, args).
  • What exactly does Selenium Webdriver’s JavascriptExecutor do?

Through the Selenium WebDriver, JavaScript is executed using the JavaScriptExecutor interface. JavascriptExecutor is necessary to use this function in Selenium because JavaScript is a programming language that communicates with HTML in a browser. The JavascriptExecutor Offers Two Techniques: ExecuteScript. ExecuteAsyncScript.

  • What is query selector selenium?

Selenium Query Selector techniques. The initial HTML Element can be located using the QuerySelector() method in the HTML DOM. The findElement() function of this approach is comparable to that of selenium.

  • What are Javascriptexecutor methods in selenium?

ExecuteScript is one of two methods offered by JavascriptExecutor. ExecuteAsyncScript.

  • How to Get text using javascript executor in selenium Webdriver?

Utilization in Selenium code

  • JavascriptExecutor Set the text with jsExecutor = (JavascriptExecutor) driver.
  • executeScript( jsExecutor “getElementById(“firstName”) / Retrieve the text from the document.
  • the string text (String) executeScript( jsExecutor “a document is returned. getElementById(“firstName” System. out. print(text);

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog