Some time we need to execute JavaScript function of application using webdriver or need to inject a piece of java script code to perform some action in application.
Webdriver provide a “JavascriptExecutor “class for executing java script, Here is step and code for the same.
First you need to create “JavascriptExecutor” class object.
JavascriptExecutor js =
(JavascriptExecutor) driver;
Now call “executeScript()”
method by passing java script code as a argument.
js .executeScript(“java script code”)
Here are some more examples.
String readyState = (String)js.executeScript("return
document.readyState");
String title = (String)js.executeScript("return
document.title");
String domain = (String)js.executeScript("return
document.domain");
WebElement el =
(WebElement)js.executeScript("return
document.getElementById('rentc');");
js.executeScript("return
document.getElementById('submit').click();");