Execute JavaScript code using Selenium Webdriver

Selenium Webdriver provides JavascriptExecutor interface to execute the Javascript on the browser window. this is one of good feature of selenium which allows you to execute client side javascript.

So lets create code for it. in this code we will cast the JavascriptExecutor to selenium webdriver.

public void exexutejavascript()
driver.get ("url name");
JavascriptExecutor jscript = (JavascriptExecutor) driver;
// now you are ready to execute the javascript. following JavaScript will return the Title of window. this is just an example.
String winTitle = (String) jscript.executeScript("return document.title");

You might also like More from author