11

[ERROR - 2016-01-16T02:22:00.898Z] Session [e6651a90-bbf7-11e5-9061-cff578894101] - page.onError - msg: ReferenceError: Can't find variable: data

:262 in error [ERROR - 2016-01-16T02:22:00.898Z] Session [e6651a90-bbf7-11e5-9061-cff578894101] - page.onError - stack: (anonymous function) (http://www.example.com/ns/common/jquery/jquery.cartActions.js?cd=0:205) o (http://www.example.com/images/common/jquery/jquery.latest.js:2) fireWith (http://www.example.com/images/common/jquery/jquery.latest.js:2) w (http://www.example.com/images/common/jquery/jquery.latest.js:4) d (http://www.example.com/images/common/jquery/jquery.latest.js:4) openUrl (:0) open (:280) (anonymous function) (:/ghostdriver/request_handlers/session_request_handler.js:495) _execFuncAndWaitForLoadDecorator (:/ghostdriver/session.js:212) _postUrlCommand (:/ghostdriver/request_handlers/session_request_handler.js:494) _handle (:/ghostdriver/request_handlers/session_request_handler.js:91) _reroute (:/ghostdriver/request_handlers/request_handler.js:61) _handle (:/ghostdriver/request_handlers/router_request_handler.js:78) :262 in error

^Domain edited out on purpose.

According to Can't find variable - PhantomJS this error has to do with not having proper jailed execution of Javascript. I don't understand what this means in the context of my Java program.

My Selenium program has only one kind of Javascript call, and it works like this:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", buttonToClick);

The line above doesn't seem to be the issue because from my tests I can see that multiple lines like the above execute without error before coming to the above error.

Also, Session.NegotiatedCapabilities has "acceptSslCerts":false, which I have not been able to solve with this code block as the PhantomJS driver initializer:

String[] cli_args = new String[]{"--debug=false", "--web-security=false", "--ssl-protocol=any", "--ignore-ssl-errors=true"};
        DesiredCapabilities caps = DesiredCapabilities.phantomjs();
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli_args);
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/Users/richard/Downloads/phantomjs-2.0.0-macosx/bin/phantomjs");
        driver = new PhantomJSDriver(caps);

I can see that the arguments ARE being passed in on the Console...

Jan 16, 2016 6:23:40 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--web-security=no, --ignore-ssl-errors=yes, --webdriver=33238, --webdriver-logfile=/Users/richard/YeezyBot/phantomjsdriver.log]

Finally, everything works with Firefox WebDriver.

Towkir
  • 3,889
  • 2
  • 22
  • 41
user3180
  • 1,369
  • 1
  • 21
  • 38
  • 1
    To anyone reading this, I have to conclude that PhantomJS feels like a premature software. Had to figure out so many hacks in order to get basic stuff working. Do not recommend. – user3180 Nov 18 '16 at 09:13
  • whats the alternative for headless browser then? – Max Barrass Jun 01 '17 at 13:27
  • 1
    you can use chrome in headless mode. https://stackoverflow.com/questions/53657215/running-selenium-with-headless-chrome-webdriver – C.Aglar Jan 07 '20 at 21:50

1 Answers1

0

Injecting JavaScript to the web page or any other element is poor practice. You can find the Element using Selenium code and click on it without the use of any injection.

  1. Wait for the page to be loaded

  2. Find the button by CSS or Xpath expression

  3. Wait for the element to by clickable

  4. click on it using Selenium code only

    WebDriverWait wait = new WebDriverWait(driver, timeToWait);

    this.by = by;

    try {

    webElement lastFoundElement = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

    wait.until(ExpectedConditions.elementToBeClickable(lastFoundElement ));

    new Actions(browser).moveToElement(element, offsetX, offsetY).click().build().perform();

    } catch (Exception ex) {
    }

Tal Angel
  • 1,301
  • 3
  • 29
  • 63