9

What is JavaScript Executor in Selenium WebDriver?

What is the use of it and how can we use this in Selenium WebDriver?

An example would be appreciated.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elegant Student
  • 305
  • 2
  • 8
  • 16
  • They actually misspelled JavaScript: *[JavascriptExecutor](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html)* – Peter Mortensen Nov 14 '22 at 23:31

6 Answers6

10

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:


Reference

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


tl;dr

Cross-domain policy file specification

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3

The long and short answer is:

It's a Selenium interface which directly lets you interact with the HTML DOM of the webpage. It does so by executing JavaScript expressions using the following syntax:

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

JavascriptExecutor provides a way to automate a user interaction even when the 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 isn't any standard way of doing it via Selenium.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
0

You can browse Interface JavascriptExecutor to get more information:

Indicates that a driver can execute JavaScript, providing access to the mechanism to do so. Because of cross domain policies browsers enforce your script execution may fail unexpectedly and without adequate error messaging. This is particularly pertinent when creating your own XHR request or when trying to access another frame. Most times when troubleshooting failure it's best to view the browser's console after executing the WebDriver request.

Basically, JavaScript Executor allows you to execute JavaScript code from Selenium WebDriver.

Example: Scrolling to element with JavaScript.

element = driver.findElement(By.id("test"));
(JavascriptExecutor) driver.executeScript("arguments[0].scrollIntoView(true);", element);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Infern0
  • 2,565
  • 1
  • 8
  • 21
0

JavaScriptExecutor is an interface that is used to execute JavaScript code through Selenium WebDriver. It provides two methods, “executescript” & “executeAsyncScript”:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.scrollBy(0,1200)");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Well, they actually misspelled JavaScript: [JavascriptExecutor](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html) – Peter Mortensen Nov 14 '22 at 23:31
0

JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. Sometimes we face a lot of issues when performing actions on web elements. Then this concept comes into the picture. Required package:

import org.openqa.selenium.JavascriptExecutor;

Methods of JavaScriptExecutor:

  1. executeAsyncScript:

    executeAsyncScript(java.lang.String script, java.lang.Object… args)

Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.

  1. executeScript:

    executeScript(java.lang.String script, java.lang.Object… args)

Executes JavaScript with below Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

Please refer to the link for more details. How to apply this interface based on different situations discussed here.

arpita biswas
  • 144
  • 1
  • 6
  • Well, they actually misspelled JavaScript: [JavascriptExecutor](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html) – Peter Mortensen Nov 14 '22 at 23:33
-1

My solution in Selenium 3:

 driver.execute_script("window.open()")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peiyu
  • 1