selenium-webdriver

Executing Javascript in the page

Syntax#

  • object ExecuteAsyncScript(string script, params object[] args);
  • object ExecuteScript(string script, params object[] args);

C#

In order to execute JavaScript in a IWebDriver instance you need to cast the IWebDriver to a new interface, IJavaScriptExecutor

IWebDriver driver;
IJavaScriptExecutor jsDriver = driver as IJavaScriptExecutor;

You can now access all the methods available on the IJavaScriptExecutor instance which allow you to execute Javascript, for example:

jsDriver.ExecuteScript("alert('running javascript');");

Python

To execute Javascript in python, use execute_script("javascript script here"). execute_script is called on a webdriver instance, and can be any valid javascript.

from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_script("alert('running javascript');")

Java

To execute Javascript in Java, create a new webdriver that supports Javascript. To use the executeScript() function, either the driver must be cast to a JavascriptExecutor, or a new variable can be set to the value of the casted driver: ((JavascriptExecutor)driver). driver.executeScript() takes in a String that is valid Javascript.

WebDriver driver = new ChromeDriver();
JavascriptExecutor JavascriptExecutor = ((JavascriptExecutor)driver);
JavascriptExecutor.executeScript("alert('running javascript');");

Ruby

require "selenium-webdriver"

driver = Selenium::WebDriver.for :chrome
driver.execute_script("alert('running javascript');")

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow