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?

asked Dec 15, 2014 at 19:35

2 Answers 2

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");
answered Dec 16, 2014 at 1:45
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.

answered Jul 25, 2019 at 5:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.