import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class WebDriverExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Java\\Lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://google.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
Object pageSource = js.executeScript("return document.body.innerHTML;",null).toString();
System.out.println(pageSource);
driver.close();
}
}
The snippet above is causing a NullPointerException.
Exception in thread "main" java.lang.NullPointerException at java.util.Arrays.stream(Unknown Source) at java.util.stream.Stream.of(Unknown Source) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:484) at WebDriverExample.main(WebDriverExample.java:25)
If I remove the optional object parameter, it leads to a compilation error.
Updated Code:
JavascriptExecutor js = (JavascriptExecutor) driver;
Object pageSource = js.executeScript("return document.body.innerHTML;").toString();
Error Message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String)
This code was tested using Selenium-server-standalone-3.141.59.jar