What is JavaScriptExecutor in Selenium?

What is JavaScriptExecutor?

JavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium driver. It provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window. 

JavaScriptExecutor in Selenium
Lets inject Javascript into Browser using Selenium

Why we use it?

To enhance the capabilities of the existing scripts by performing javascript injection into our application under test.

In simple words  “Javascript can be executed within the browser with the help of JavaScript Executor.”


import org.openqa.selenium.JavascriptExecutor;


JavascriptExecutor js = (JavascriptExecutor) driver;  

script – The JavaScript to execute
Arguments – The arguments to the script.(Optional)


1.How to generate Alert Pop window in selenium?


JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('hello world');");

2.How to click a button in Selenium WebDriver using JavaScript


JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

3.How to refresh browser window using Javascript ?


JavascriptExecutor js = (JavascriptExecutor)driver;

4.How to get innertext of the entire webpage in Selenium?


JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.documentElement.innerText;").toString();

5.How to get the Title of our webpage ?


JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.title;").toString();

6.How to perform Scroll on application using  Selenium


  JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 50 pixels

Note:- for scrolling till the bottom of the page we can use the code like


7.How to click on a SubMenu which is only visible on mouse hover on Menu?


    JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Automation Menu on the MenuBar
js.executeScript("$(' li').hover()");

8.Implement Highlight in Selenium?

9.How to navigate to different page using Javascript?


JavascriptExecutor js = (JavascriptExecutor)driver;
//Navigate to new Page
js.executeScript("window.location = ''");

Working Demo:-

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JavaScriptExecuter {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
//Launching the browser application
//Adding wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize window
//Creating the Javascriptexecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//Fetching the Domain Name
String sDomain = js.executeScript("return document.domain;").toString();
System.out.println("Domain = "+sDomain);
//Fetching the URL
String sURL = js.executeScript("return document.URL;").toString();
System.out.println("URL = "+sURL);
//Fetching the Title
String sTitle = js.executeScript("return document.title;").toString();
System.out.println("Title = "+sTitle);
//Vertical scroll - down by 200 pixels
System.out.println("Successfully did the vertical scroll by 200px");



28 replies on “What is JavaScriptExecutor in Selenium?”

Below warning is displayed for the code line "String sDomain = js.executeScript("return document.domain;").toString();" in Eclipse editor

"The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String)"

I am getting this error for "js.executescript" in other code too.

Please help. Thanks in advance.

How can i click on a player's play button through java and selenium, it's ooyala player that i'm working on.

Very nice article, thank you for sharing.
I have a question, if some attributes of tag made unselectable="on" and/or style="display: none", selenium webdriver not performing some actions on these elements. shall we use javascriptexecutor to forcibly perform those operations?
example, in below html code, we can observe select element does not allow selection of an item…
i) span class="k-dropdown-wrap k-state-default k-state-focused" unselectable="on"
ii) span class="k-select" unselectable="on"
select id="state-of-garageAddress" class="form-control" _ngcontent-kqf-1="" data-role="dropdownlist" style="display: none;"
option value="null"
option value="1" AL
option value="2" AK
option value="3" AZ


-* removed tags as its not allowing to post html format

if so could you please post code snippet for same
Thanks again

youtube webpage having 2 scroll bars in active state,one scroll bar is related to html source code and other one is non html element.i want to write code for non html scroll bar element..could anyone please suggest the code for that..

Leave a Reply

Your email address will not be published. Required fields are marked *