javascript - Ghostdriver 1.2.1 + PhantomJS 2.0 + 最新的 Selenium 在 Java 中找不到变量错误

标签 javascript java selenium phantomjs ghostdriver

[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



^域名被故意删掉。

根据Can't find variable - PhantomJS此错误与未正确执行 Javascript 有关。我不明白这在我的 Java 程序上下文中意味着什么。

我的 Selenium 程序只有一种 Javascript 调用,它的工作方式如下:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", buttonToClick);

上面的行似乎不是问题,因为从我的测试中我可以看到像上面这样的多行在出现上述错误之前执行没有错误。

此外,Session.NegotiatedCapabilities 具有“acceptSslCerts”:false,我无法使用此代码块作为 PhantomJS 驱动程序初始化程序来解决此问题:
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);

我可以看到参数正在控制台上传递......
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]

最后,一切都适用于 Firefox WebDriver。

最佳答案

将 JavaScript 注入(inject)网页或任何其他元素是不好的做法。您可以使用 Selenium 代码找到 Element 并单击它,而无需使用任何注入(inject)。

  • 等待页面加载
  • 通过 CSS 或 Xpath 表达式查找按钮
  • 等待元素可点击
  • 仅使用 Selenium 代码单击它
    WebDriverWait 等待 = 新的 WebDriverWait(驱动程序,timeToWait);
    this.by = by;
    尝试 {
    webElement lastFoundElement =
    wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    wait.until(ExpectedConditions.elementToBeClickable(lastFoundElement));
    new Actions(browser).moveToElement(element, offsetX, offsetY).click().build().perform();
    }
    捕捉(异常(exception)前)
    {
    }
  • 关于javascript - Ghostdriver 1.2.1 + PhantomJS 2.0 + 最新的 Selenium 在 Java 中找不到变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823870/

    相关文章:

    javascript - 模态打开时执行某些操作

    java - Firebase RecyclerView 显示一项

    java - 如何测试使用 Selenium 的 Java 代码?

    java - Selenium 找不到 <p :calendar> day by using By. linkText()

    python - 引发 TimeoutException(消息、屏幕、堆栈跟踪)TimeoutException : Message:

    selenium - 选择兄弟元素的子元素

    javascript - 以下 javascript 代码将原因值变为 false

    javascript - 根据页面滚动更改div位置

    javascript - 如何在收到获取请求后重新加载 CSS 或按钮或页面的一部分?

    java - Apache Tika 停止日志记录