Javascript Executor
JavascriptExecutor is an interface available in the package org.openqa.selenium.
This interface is implemented by classes ChromeDriver, EdgeDriver, EventFiringWebDriver,
FirefoxDriver, InternetExplorerDriver, OperaDriver, RemoteWebDriver, SafariDriver
This interface is implemented by classes ChromeDriver, EdgeDriver, EventFiringWebDriver,
FirefoxDriver, InternetExplorerDriver, OperaDriver, RemoteWebDriver, SafariDriver
1. Executes JavaScript in the context of the currently selected frame or window.
2. The script fragment provided will be executed as the body of an anonymous function.
3. Within the script, use document to refer to the current document.
4. To interact with Highcharts: Interactive JavaScript charts for your webpage,
JavascriptExecutor will be helpful.
Reference Link->https://www.highcharts.com/
JavascriptExecutor Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package org.totalqa.selenium; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class JavascriptEx { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.get("https://www.highcharts.com/"); JavascriptExecutor jse = (JavascriptExecutor) driver; //Fetching the title of the webpage String title = (String) jse.executeScript("return document.title"); System.out.println("Title::" + title); //Verify the ReadyState of the Webpage String readyState = (String)jse.executeScript("return document.readyState"); System.out.println("readyState::" + readyState); //Scroll jse.executeScript("scroll(400,600)"); Thread.sleep(5000); String value = (String) jse.executeScript("return document.getElementsByClassName('highcharts-point highcharts-color-1')[0].getAttribute('d')"); System.out.println("The values plotted in the chart::"+ value); //Scroll to the Element View jse.executeScript("document.getElementById('hc-item').scrollIntoView('true')"); WebElement e1 = driver.findElement(By.id("hc-item")); jse.executeScript("arguments[0].scrollIntoView(true)", e1); WebElement e2= driver.findElement(By.id("hs-item")); //Multiple Elements are focused into view jse.executeScript("arguments[0].scrollIntoView(true);arguments[1].scrollIntoView(true)",e1,e2); } } |
Console Output:
1 2 3 |
Title::Interactive JavaScript charts for your webpage | Highcharts readyState::complete The values plotted in the chart::M 17 213 A 4 4 0 1 1 16.99999800000017 212.99600000066667 Z |