Table of Contents

Interface IJavaScriptExecutor

Namespace
OpenQA.Selenium
Assembly
WebDriver.dll

Defines the interface through which the user can execute JavaScript.

public interface IJavaScriptExecutor

Methods

ExecuteAsyncScript(string, params object[])

Executes JavaScript asynchronously in the context of the currently selected frame or window.

object ExecuteAsyncScript(string script, params object[] args)

Parameters

script string

The JavaScript code to execute.

args object[]

The arguments to the script.

Returns

object

The value returned by the script.

ExecuteScript(PinnedScript, params object[])

Executes JavaScript in the context of the currently selected frame or window.

object ExecuteScript(PinnedScript script, params object[] args)

Parameters

script PinnedScript

A PinnedScript object containing the code to execute.

args object[]

The arguments to the script.

Returns

object

The value returned by the script.

Remarks

The ExecuteScript method executes JavaScript in the context of the currently selected frame or window. This means that "document" will refer to the current document. If the script has a return value, then the following steps will be taken:

  • For an HTML element, this method returns a IWebElement
  • For a number, a long is returned
  • For a boolean, a bool is returned
  • For all other cases a string is returned.
  • For an array,we check the first element, and attempt to return a List<T> of that type, following the rules above. Nested lists are not supported.
  • If the value is null or there is no return value, null is returned.

Arguments must be a number (which will be converted to a long), a bool, a string or a IWebElement, or a IWrapsElement. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"

Exceptions

ArgumentNullException

If script is null.

ExecuteScript(string, params object[])

Executes JavaScript in the context of the currently selected frame or window.

object ExecuteScript(string script, params object[] args)

Parameters

script string

The JavaScript code to execute.

args object[]

The arguments to the script.

Returns

object

The value returned by the script.

Remarks

The ExecuteScript method executes JavaScript in the context of the currently selected frame or window. This means that "document" will refer to the current document. If the script has a return value, then the following steps will be taken:

  • For an HTML element, this method returns a IWebElement
  • For a number, a long is returned
  • For a boolean, a bool is returned
  • For all other cases a string is returned.
  • For an array,we check the first element, and attempt to return a List<T> of that type, following the rules above. Nested lists are not supported.
  • If the value is null or there is no return value, null is returned.

Arguments must be a number (which will be converted to a long), a bool, a string or a IWebElement, or a IWrapsElement. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"