1

I was wondering if anyone could help me find a way to output to the Eclipse console the results returned from executing a Javascript ?

These are the steps I manually perform (that I would like to automate):

  1. Open test web page.
  2. Open Google chrome Javascript console
  3. Type "productObj.mainURL" and enter

This will output to the Chrome Javascript console the URL value/attribute that I'm looking for.

I've done a fair bit of research on my own, and I am stumped.

I've tried the code below:

JavascriptExecutor js = (JavascriptExecutor) driver;
String mainURL = (String) js.executeScript("productObj.mainURL");
System.out.println(mainURL);

But all I get returned is a "null"...

Any help please?

5

You need to tell it to return the value.

Change this line:

String mainURL = (String) js.executeScript("productObj.mainURL");

to:

String mainURL = (String) js.executeScript("return productObj.mainURL");
| improve this answer | |
1

String mainURL = (String) js.executeScript("return productObj.mainURL"); will give errors for some String.

String mainURL = js.executeScript("return productObj.mainURL").toString(); is better I guess.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.