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");

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

Any help please?

2 Answers 2


You need to tell it to return the value.

Change this line:

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


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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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