What is JavaScriptExecutor in Selenium?

JavascriptExecutor

JavascriptExecutor is the Selenium interface which is being implemented by all the following classes:

  • FirefoxDriver
  • ChromeDriver
  • InternetExplorerDriver
  • EdgeDriver
  • OperaDriver
  • SafariDriver
  • RemoteWebDriver
  • EventFiringWebDriver
  • HtmlUnitDriver

While you execute your Selenium script at times because of cross domain policies browsers enforce your script execution may fail unexpectedly and without adequate error logging. This is particularly pertinent when creating your own XHR request or when trying to access another frame.

You will find a detailed discussion in Uncaught DOMException: Blocked a frame with origin “http://localhost:8080” from accessing a cross-origin frame while listing the iframes in page

JavascriptExecutor interface provides two methods as follows:

  • executeScript(): This method executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function. Within the script you need to use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist.

  • executeAsyncScript(): This method executes an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.


Example

A couple of examples:

  • Using JS to enter text

    String js = "arguments[0].setAttribute('value','"+inputText+"')"
    ((JavascriptExecutor) webDriver).executeScript(js, element);
    
  • Double click through JavaScript

    new Actions(driver).moveToElement(myElem, posX, posY).perform();
    ((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY);
    
  • Sending variable character strings through executeScript()

    String myValue = "80120804076";
    WebElement pesel = driver.findElement(fldPesel);
    jse.executeScript("arguments[0].value='" + myValue + "';", pesel);
    

Reference

You also can find a couple of detailed discussions about the arguments in:

  • What does arguments[0] and arguments[1] mean when using executeScript method from JavascriptExecutor interface through Selenium WebDriver?

tl;dr

Cross-domain policy file specification


Long and short answer is:

It's a Selenium Interface which directly lets you Interact with HTML DOM of the webpage, it does so by executing JavaScript expressions using Following Syntax :

(JavascriptExecutor) driver.executeScript("JavaScript_EXPRESSION_HERE", ADDITIONAL_ARGUMENTS);

JavascriptExecutor provides a way to automate a user interaction even when page is not essentially loaded completely or elements are placed in a way that the direct interaction is blocked.

This however is also the disadvantage too, if you want to automate a webpage as if a real user experience. That said, although it is a really powerful option, but we should try not to use JavaScript Executor unless there is no standard way of doing it via Selenium.