Execute JavaScript code using Selenium Webdriver

- Advertisement -

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");

- Advertisement -

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");

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More