Javascript Executor In Selenium Java

Javascript is a language used to perform interaction between HTML dom and browser. In selenium webdriver, locatiors like ID, css, xpath are used to identify webelements. There might come a situation when these locators do not work or webdriver throws exceptions while trying to perform any operations on some elements. To overcome these issues, we can use javaScriptExecutor supported by selenium. To execute javascript in a webdriver session, no extra addon or plugin is required. It is present in

org.openqa.selenium.JavascriptExecutor package.

What is JavaScriptExecutor

JavaScriptExecutor is a class that provide mechanism to execute javascript through Selenium Webdriver. It provides two methods: executescript & executeAsyncScript.

  1. executeAsyncScript

async script do not force users to wait for script to download before page is loaded. Async script renders page quickly. The JS so executed is single-threaded with a various callback function which runs synchronously.

  1. Executescript

The script used in this method runs in the body of an anonymous function (a function without a name). We can also pass complicated arguments to it. 

Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver; 

js.executeScript(<script>,<arguments>);

It may return: Boolean, Long, String, List, WebElement or null.


Below are examples of various operations performed by JavascriptExecutor:

  • Generate alert popup window

Sometimes it is required to generate javascript in a browser session

JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript("alert('hello world');");
  • Refresh browser

In selenium, web page can be refreshed in different ways such as by using refresh command or by using F5 (key) at current browser session. Below is the code snippet to refresh page using class- javascript Executer:

JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");
  • Scroll page

Sometime webelement is present in web page and even if correct locator is defined, webdriver session is not able to perform any operation on Webelement.

In this case, we can scroll webelement into view and then perform any operation.

JavascriptExecutor js =(JavascriptExecutor)driver;

js.executeScript("arguments[0].scrollIntoView(true);",element);
  • Enable/Disable webelement

This situation occurs when sometimes due to a defect say, a textbox is disabled on browser and we want automation script to continue. Here solution would be to enable the textbox and then continue tests.

To enable element, use syntax similar as below

JavascriptExecutor js =(JavascriptExecutor)driver;

String toenable = "document.getElementsByName('firstname')[0].removeAttribute('disabled');";

js.executeScript(toenable);

 

To disable element, use syntax similar as below

JavascriptExecutor js =(JavascriptExecutor)driver;

String todisable = "document.getElementsByName('fname')[0].setAttribute('disabled', '');";

js.executeScript(todisable);

To get inner text of the entire webpage in Selenium

JavascriptExecutor js = (JavascriptExecutor)driver;

String innertext = js.executeScript("return document.documentElement.innerText;").toString();

System.out.println(innertext);

  • Get current state of checkbox

To find whether a checkbox is selected or not, use code similar as below

JavascriptExecutor js = (JavascriptExecutor) driver;

Boolean isChecked = (Boolean) js.executeScript("return $(\'.mycheckbox\').is(\":checked\")");

In similar way, you can execute any javascript command using selenium webdriver. I hope it would be helpful. Kindly provide your feedback in comments.

To view or add a comment, sign in

More articles by Ankit A.

  • How can a standing desk polish up your work?

    Sitting is the new smoking and sitting too long is inadequate for your health. Sitting continuously increases risk of…

    1 Comment
  • 10 Tips for Working from Home

    With the covid-19 pandemic, people’s lives have transformed professionally and individually. When it comes to work…

    1 Comment
  • Selenium 4: New Changes and Enhancements

    In August 2018, Simon Stewart, the founding member of Selenium, had officially announced selenium 4 release date and…

  • 10 Selenium Webdriver Tips and Tricks

    Many times we face some simple issues while working with Selenium Webdriver. Few simple steps can increase the…

    1 Comment

Others also viewed

Explore content categories