0

I can see these many properties in console if I type window in Chrome console and hit enter.

But when I am trying to access same properties via Javascript Executor it gives me null.

I tried:

String homepage = jse().executeScript("window.origin", "found homepage").toString();
System.out.println("home page is  "+homepage);

and

String location = "function show_homepage() {var homepage = window.location.origin;return homepage;}"
String homepage = jse().executeScript(location, "found if email validated or not").toString();
System.out.println("Answer is "+homepage);

Reference:

  1. window object from selenium webdriver is empty array
  2. https://www.softwaretestingmaterial.com/javascriptexecutor-selenium-webdriver/
  3. View list of all JavaScript variables in Google Chrome Console

enter image description here

paul
  • 4,333
  • 16
  • 71
  • 144
  • 1
    Java is to JavaScript as a Car is to a Carpet. don't tag Java for a JS question – Stultuske May 10 '19 at 08:22
  • Isn't that java code that I wrote. Viewers must know what language I am using for scripting. Also, I wanted java crowd to take a look at question, because Java developers uses `Javascript` pretty often. Isn't that the purpose of tagging, to send questions to particular community/crowd? – paul May 10 '19 at 08:27

1 Answers1

0

I found the solution. The mistake I was doing, I was not returning function in JS snippet. I added one more line return show_homepage(); and it worked.

String location = "function show_homepage() {"+
            "var homepage = window.location.origin;"+
            "return homepage;"+
            "}"+
            "return show_homepage();";
Object str = js().executeScript(location, "");
System.out.println(str.toString());
paul
  • 4,333
  • 16
  • 71
  • 144