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

提问人:user3180 提问时间:1/16/2016 最后编辑:Towkiruser3180 更新时间:7/21/2021 访问量:1644

问:

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

: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) 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) 开盘 (: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 错误

^域名是故意编辑的。

根据 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 java selenium phantomjs 幽灵驱动程序

评论

1赞 user3180 11/18/2016
对于任何读到这篇文章的人来说,我不得不得出结论,PhantomJS 感觉像是一个不成熟的软件。为了让基本的东西正常工作,必须弄清楚这么多的技巧。不推荐。
0赞 Max Barrass 6/1/2017
那么无头浏览器的替代方案是什么呢?
1赞 C.Aglar 1/8/2020
您可以在无头模式下使用 Chrome。stackoverflow.com/questions/53657215/......

答:

0赞 Tal Angel 7/21/2021 #1

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

  1. 等待页面加载

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

  3. 等待元素通过可点击

  4. 仅使用 Selenium 代码单击它

    WebDriverWait wait = new WebDriverWait(driver, timeToWait);

    this.by = 由;

    尝试 {

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

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

    new Actions(浏览器).moveToElement(element, offsetX, offsetY).click().build().perform();

    } catch (例外 ex) {
    }