What is JavaScript ?
Java Script is an object-oriented programming language commonly used inside the browser to interact with HTML DOM. This means that a Browser has JavaScript implementation in it and understands the JavaScript commands. Selenium WebDriver gives you a method called executeScript which executes the JavaScript in context of the loaded browser page.
What is JavaScriptExecutor ?
- The JavaScriptExecutor is an interface that provides a mechanisms to execute JavaScript through the Selenium Driver. To run JavaScript in the context of the currently selected frame or window it provides “
executeScript” and “executeAsyncScript” methods. - Sometimes the locator can not work, in that case the JavaScriptExecutor will help to perform desired action on a web elements on a particular webpage.
- JavaScript extractor is also very useful for identifying hidden elements and interacting with them on the webpage.
- There is no need for an extra plugin or add-on. You just need to import org.openqa.selenium.JavascriptExecutor in the script as to use JavaScriptExecutor.
JavaScriptExecutor Methods
executeAsyncScript(String script,args)Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.executeScript(String script,args)Executes JavaScript in the context of the currently selected frame or window.
For more details click here

Syntax
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
Parameters:script – The JavaScript to executeargs – The arguments to the script. May be empty
Returns: One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
How to generate Alert Pop window in selenium ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("alert('hello world!');");
How to get innertext of the entire webpage in Selenium ?
JavascriptExecutor js = (JavascriptExecutor) driver;
String sText = js.executeScript("return document.documentElement.innerText;").toString();
How to navigate to different page using JavaScript ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.location = 'https://www.stqatools.com/'");
How to enter value into textbox ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector('#btn').click()");
How to refresh a window ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("history.go(0)");
How to get the text of a particular web element ?
JavascriptExecutor js = (JavascriptExecutor) driver;
String text = js.executeScript("return document.getElementById('btn').innerHTML").toString();
System.out.println("WebElement Text is : " + text);
How to get the title of the WebPage ?
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = js.executeScript("return document.title").toString();
System.out.println("Page Title is : " + text);
How to scroll vertically for certain pixels ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");
How to scroll till the bottom of the web page ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
How to scroll to a particular element ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector('#countries').scrollIntoView()");
How to navigate back to page ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.history.back()");
How to navigate to forward page ?
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.history.forward()");
Example of JavaScriptExecutor in Selenium :
Lets wrap all into one example as shown as below-
package automationTestingLab;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaScriptExecutor {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://automationtesting.code.blog/");
driver.findElement(By.className("accept")).click();
// Creating the JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor) driver;
// How to generate Alert Pop window in selenium ?
js.executeScript("alert('Welcome to Automation Testing Lab !!');");
Thread.sleep(3000);
// Accept the alert
Alert alert = driver.switchTo().alert();
System.out.println("===Alert Text ==="+alert.getText());
alert.accept();
// How to get innertext of the entire webpage in Selenium?
String sText = js.executeScript("return document.documentElement.innerText;").toString();
System.out.println(sText);
// How to navigate to different page using Javascript ?
js.executeScript("window.location = 'https://automationtesting.code.blog/testng/'");
// How to enter value into textbox
js.executeScript("document.getElementById('comment').value=Automation Testing Lab'");
//Automation Testing Lab
// How to click a button:
js.executeScript("document.getElementById('comment').click()");
// How to refresh a window:
js.executeScript("history.go(0)");
// How to get the text of a particular web element:
String text = js.executeScript("return document.getElementById('comment').innerHTML").toString();
System.out.println("WebElement Text is : " + text);
// How to get the title of the WebPage:
String title = js.executeScript("return document.title").toString();
System.out.println("Page Title is : " + title);
// How to scroll vertically for certain pixels:
js.executeScript("window.scrollBy(0,500)");
// How to scroll till the bottom of the web page:
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
// How to scroll to a particular element:
js.executeScript("document.getElementById('comment').scrollIntoView()");
// How to navigate back to page:
js.executeScript("window.history.back()");
// How to navigate to forward page:
js.executeScript("window.history.forward()");
}
}