2
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;       

public class oo {
   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 s = js.executeScript("return document.body.innerHTML;",null).toString();

      System.out.println(s);
      driver.close();
   }
}

Above code returns 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 oo.main(oo.java:25)

When I remove optional object parameter, it goes compilation error.

Code:

  JavascriptExecutor js = (JavascriptExecutor) driver;  
  Object s = js.executeScript("return document.body.innerHTML;").toString();

Error:

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)

Using Selenium-server-standalone-3.141.59.jar

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Amresh
  • 315
  • 1
  • 5
  • 14
  • 1
    When I remove optional object parameter, it goes compilation error. -> if it stops compiling, the parameter is not optional – Stultuske Nov 21 '18 at 12:09
  • What's the meaning of s Object? It will be propably always null. – pburgr Nov 21 '18 at 12:18
  • do you really need to use an external API? I mean, with Java 8, there is Nashorn – UninformedUser Nov 21 '18 at 12:29
  • 1
    did you try `new Object[]{}` as second param? The docs say "may be empty" which doesn't mean "may be null" – UninformedUser Nov 21 '18 at 12:31
  • It doesnt allow to remove second optional 'arguments' parameter. Goes for compilation error. – Amresh Nov 21 '18 at 12:36
  • Did you try something like `driver.findElementByTagName("body").getAttribute("innerHTML").toString()`? – Andersson Nov 21 '18 at 12:41
  • 1
    The `NullPointerException` is because you are sending `null` as args. Did you try without it? – Guy Nov 21 '18 at 12:50
  • Without null as second argument, it goes compilation error. But as per syntax, arguments list is optional. It's confusing. – Amresh Nov 21 '18 at 13:32
  • Object executeScript(String script, Object... args); It can be called with a single param of a String type. Just checked it. Are you sure you got a compilation error? – Vladimir Efimov Nov 21 '18 at 13:49
  • Yes I am sure I am getting compilation error. The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String) – Amresh Nov 21 '18 at 18:06

1 Answers1

1

To extract and print the Page Source through JavascriptExecutor you can use the following (Java based) solution:

  • Syntax:

    String page_source = ((JavascriptExecutor)driver).executeScript("return document.documentElement.innerHTML;").toString();
    System.out.println(page_source);
    

Note: You need to induce a waiter for the page to load completely before extracting the Page Source.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352