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.”

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");

}

}


ufthelp

28 thoughts on “What is JavaScriptExecutor in Selenium?

  1. 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.

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

  3. 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

  4. 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 *