3

Main goal is to get javascript element authKey into Java code. Here is code:

index.html:

...
      <script type="text/javascript">

        var params = {
            authKey: "abc"
        };

        Main.init("#content", params);
    </script>
...

main.java:

    public static void main(String[] args) throws InterruptedException {

        WebDriver d = new FirefoxDriver();
        d.get("***/index.html");
   //     System.out.println(" var " + d.findElement(By.xpath("/html/body/script[3]")));
        Thread.sleep(3000);
        JavascriptExecutor jse = (JavascriptExecutor) d;
//        System.out.println(jse.executeAsyncScript("document.URL"));
        Object val = jse.executeScript("return params.authKey;");
        d.quit();
    }

I'm always getting something like:

Exception in thread "main" org.openqa.selenium.JavascriptException: ReferenceError: **params is not defined**
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Mikhails-MacBook-Pro.local', ip: '10.10.20.139', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.6', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver

I've tried different ways to get this authKey parameter, without return, as function, but still nothing... Could someone help with that?

Thanks in advance.

UPD: Solution https://stackoverflow.com/a/40936063/6809155 works, but still looking for native js solution to get exactly params.authKey parameter, because in future there could be a lot of params.

Community
  • 1
  • 1
  • 1
    Possible duplicate of [How to use JavaScript with Selenium WebDriver using Java](http://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-using-java) – AxelH Dec 02 '16 at 14:42
  • Random guess, it's a different JS scope and params is not attached to the "window" object. – dav1d Dec 02 '16 at 14:43
  • @AxelH thanks, but i've already tried all variants from there. And from http://stackoverflow.com/questions/13994393/reading-javascript-variables-using-selenium-webdriver – Mikhail Zlochevskyi Dec 02 '16 at 16:00

2 Answers2

2

There's a scope issue depending on the version of Firefox. It could be that your variable is sand boxed in window.wrappedJSObject.

Try this:

Object val = jse.executeScript(
  "return (window.params || window.wrappedJSObject.params).authKey;");
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

Assuming you got only one script tag or this is the first one.

    WebElement scr = driver.findElement(By.tagName("script"));
    System.out.println(scr.getAttribute("innerHTML"));
    System.out.println(scr.getAttribute("innerText"));

You should get the following output using either of the above two attributes.

var params = {
     authKey: "abc"
};

Main.init("#content", params);

Parse the output to get the authKey...

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • oh, yes, that is working solution :) thanks! **ps:** but still mostly perfect would be get exactly params.authKey value, as i can do this from Firebug console... – Mikhail Zlochevskyi Dec 02 '16 at 15:57