---
name: Execute Script
short_description: Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame (Web context only)

description:
  |
    The executed script is assumed to be synchronous and the result of evaluating the script is returned to the client.

    The script argument defines the script to execute in the form of a function body. The value returned by that function will be returned to the client. The function will be invoked with the provided args array and the values may be accessed via the arguments object in the order specified.

    Arguments may be any JSON-primitive, array, or JSON object. JSON objects that define a WebElement reference will be converted to the corresponding DOM element. Likewise, any WebElements in the script result will be returned to the client as WebElement JSON objects.



example_usage:
  java:
    |
      ((JavascriptExecutor) driver).executeScript("window.setTimeout(arguments[arguments.length - 1], 500);");

  python:
    |
      self.driver.execute_script(‘document.title’)
  javascript_wd:
    |
      await driver.safeAsync('document.title');
  javascript_wdio:
    |
      var result = browser.execute(function(a, b, c, d) {
        // browser context - you may not access client or console
        return a + b + c + d;
      }, 1, 2, 3, 4)

      // node.js context - client and console are available
      console.log(result.value); // outputs: 10
  ruby:
    |
      @driver.execute_script("document.title")
  php:
    |
      // TODO PHP sample
  csharp:
    |
      // TODO C# sample

client_docs:
  java: "https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html#executeScript-java.lang.String-java.lang.Object...-"
  python: "http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_script"
  javascript_wdio: "http://webdriver.io/api/protocol/execute.html"
  javascript_wd: "https://github.com/admc/wd/blob/master/lib/commands.js#L102"
  ruby: "http://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Driver:execute_script"
  php: "https://github.com/appium/php-client/" # TODO PHP documentation link
  csharp: "https://github.com/appium/appium-dotnet-driver/" # TODO Dotnet documentation link

# Driver support by platform
driver_support:
  ios:
    xcuitest: false
    uiautomation: false
  android:
    uiautomator2: false
    uiautomator: false
  mac:
    mac: false
  windows:
    windows: false
client_support:
  java: false
  python: false
  ruby: false
  php: false
  csharp: false
  javascript_wd: false
  javascript_wdio: false
  

# Information about the HTTP endpoints
endpoint:
  url: /wd/hub/session/:session_id/execute_async
  method: GET
  url_parameters:
    - name: session_id
      description: ID of the session to route the command to
  json_parameters:
    - name: script
      type: string
      description: The script to execute
    - name: args
      type: array
      description: The script arguments
  response:
    - type: any
      description: The script result

# Links to specifications. Should link to at least one specification
specifications: 
  w3c: https://www.w3.org/TR/webdriver/#dfn-execute-script
  jsonwp: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#sessionsessionidexecute_async
