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

Ghostdriver 1.2.1 + PhantomJS 2.0 + latest Selenium Can't find variable error in Java

本文关键字:Java 找不到 错误 变量 PhantomJS 最新 Ghostdriver Selenium      更新时间:2023-09-26

[错误 - 2016-01-16T02:22:00.898Z] 会话 [e6651a90-bbf7-11e5-9061-cff578894101] - page.onError - msg:引用错误:找不到变量:数据

:262 错误 [错误 - 2016-01-16T02:22:00.898Z]会话 [e6651a90-bbf7-11e5-9061-cff578894101] - page.onError - 堆栈: (匿名功能)(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) 火与 (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) 打开网址 (:0) 打开 (:280) (匿名功能)(:/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 错误

^域被故意编辑掉。

根据找不到变量 - 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 注入网页或任何其他元素都是不好的做法。您可以使用Selenium代码找到元素,然后单击它而无需使用任何注入。

  1. 等待页面加载

  2. 通过 CSS 或 Xpath 表达式查找按钮

  3. 等待元素通过可点击

  4. 仅使用硒代码单击它

    WebDriverWait = new WebDriverWait(driver, timeToWait);

    this.by = 通过;

    尝试 {

    webElement lastFoundElement =wait.until(ExpectConditions.visibilityOfElementTLocation(by));

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

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

    }捕获(例外,例如){
    }