selenium-webdriver Executing Javascript in the page 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');");