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):
- Open test web page.
- Open Google chrome Javascript console
- 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?
2 Answers 2
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");
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.