Skip to content

JavaScript execution in WebDriver

WebDriver provides a mechanism to execute arbitrary JavaScript code within the context of the currently selected browser window or frame. This functionality is primarily accessed through the executeScript method^[600-developer-automatic-chromedriver.md].

Core Implementation

In Java, the ability to run JavaScript is exposed via the org.openqa.selenium.remote.RemoteWebDriver.executeScript(String, Object[]) method^[600-developer-automatic-chromedriver.md].

To invoke this method, the driver instance must implement the JavascriptExecutor interface.^[600-developer-automatic-chromedriver.md] If the underlying driver does not support this feature, it will throw an IllegalStateException.^[600-developer-automatic-chromedriver.md]

Basic Pattern

The standard pattern involves casting the WebDriver instance to JavascriptExecutor before calling executeScript:^[600-developer-automatic-chromedriver.md]

if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor)driver).executeScript("yourScript();");
} else {
    throw new IllegalStateException("This driver does not support JavaScript!");
}

Use Cases

Executing JavaScript enables interactions that go beyond standard Selenium commands, such as manipulating the browser DOM directly or controlling window behavior.

Opening New Windows

JavaScript can be used to programmatically open new browser windows or tabs, which differs from standard interactions like using keyboard shortcuts.^[600-developer-automatic-chromedriver.md]

((JavascriptExecutor) driver).executeScript("window.open()");

This method allows the script to create a new window context immediately.^[600-developer-automatic-chromedriver.md] After execution, the driver can switch between the new and original windows using the getWindowHandles() method.^[600-developer-automatic-chromedriver.md]

Sources

^[600-developer-automatic-chromedriver.md]