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.
![]() |
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.”
Package:-
import org.openqa.selenium.JavascriptExecutor;
Syntax:-
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
script – The JavaScript to execute
Arguments – The arguments to the script.(Optional)
Scenario’s
1.How to generate Alert Pop window in selenium?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('hello world');");
2.How to click a button in Selenium WebDriver using JavaScript
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
3.How to refresh browser window using Javascript ?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");
4.How to get innertext of the entire webpage in Selenium?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.documentElement.innerText;").toString();
5.How to get the Title of our webpage ?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText = js.executeScript("return document.title;").toString();
6.How to perform Scroll on application using Selenium
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 50 pixels
js.executeScript("window.scrollBy(0,50)");
Note:- for scrolling till the bottom of the page we can use the code like
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
7.How to click on a SubMenu which is only visible on mouse hover on Menu?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Automation Menu on the MenuBar
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");
8.Implement Highlight in Selenium?
9.How to navigate to different page using Javascript?
Code:-
JavascriptExecutor js = (JavascriptExecutor)driver;
//Navigate to new Page
js.executeScript("window.location = 'https://www.facebook.com/uftHelp'");
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
driver.get("http://www.uftHelp.com");
//Adding wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize window
driver.manage().window().maximize();
//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
js.executeScript("window.scrollBy(0,200)");
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.
simply wonderful
What will be the parent exception that I can use for catching any exception in my code while using javaexecuter ?
simply wonderful
How to right click using java script
You can implement it by using ContextClick method of Action Class.
Please refer the below link for the example code.
http://www.ufthelp.com/2014/11/working-with-action-class-in-selenium.html
Excellent
Nice 🙂
Superb..!!!
How can I include multi-line scripts? Seems to never really resolve my code.
Thank you….who has developed this sites.
How can i post message events to my browser using this ?
How can I use one element from the elements found by
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementsByClassName('class Name');");
How can i click on a player's play button through java and selenium, it's ooyala player that i'm working on.
How to press keyboard shortcut key like Ctrl+Shit+Z
Than what is the difference using javascript and java? both are hitting the browser, i mean we can use java for selenium on browser too, then y javascript?
Superb examples
how to handle list of objects and webelements in javascriptexecutor
org.openqa.selenium.WebDriverException: arguments[0].click is not a function
getting this error
Thank you very much!!!! It is what I was looking for!!
how can i insert text into a textbox
What is the return type of JavaScriptexecutor.executescript() method???
thanks, it was very useful.
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
select
span
-* removed tags as its not allowing to post html format
if so could you please post code snippet for same
Thanks again
how to bypass a javascript error in selenium as that error is not allowing my code to execute
Hey i need to click on a date which is in pop up using java script mouse hover
by using sendkeys
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..