我用它的默认启动命令启动了一个 selenium 网格集线器,
java -jar selenium-server-standalone-2.33.0.jar -role hub
我在同一台机器上以 webdriver 模式启动 PhantomJS,比如,
phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444
当 PhantomJS 启动时,除了“所有‘OK’”消息,我什么也没有得到,
[INFO - 2013-09-09T18:10:38.849Z] GhostDriver - Main - running on port 8080
[INFO - 2013-09-09T18:10:38.850Z] GhostDriver - Main - registering to Selenium HUB 'http://127.0.0.1:4444' using '127.0.0.1:8080'
[INFO - 2013-09-09T18:11:47.164Z] HUB Register - register - Registered with grid hub: http://127.0.0.1:4444/ (ok)
但是如果我尝试使用那个浏览器,用
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.PHANTOMJS)
查看 PhantomJS 可执行文件的输出,
[INFO - 2013-09-09T18:17:12.531Z] Session [0c5c9500-197c-11e3-8eed-b3b7a73965aa] - _decorateNewWindow - page.settings: {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.2 Safari/534.34","webSecurityEnabled":true}
[INFO - 2013-09-09T18:17:12.531Z] Session [0c5c9500-197c-11e3-8eed-b3b7a73965aa] - page.customHeaders: - {}
[INFO - 2013-09-09T18:17:12.531Z] Session [0c5c9500-197c-11e3-8eed-b3b7a73965aa] - CONSTRUCTOR - Desired Capabilities: {"platform":"ANY","javascriptEnabled":true,"browserName":"phantomjs","version":""}
[INFO - 2013-09-09T18:17:12.531Z] Session [0c5c9500-197c-11e3-8eed-b3b7a73965aa] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.2","driverName":"ghostdriver","driverVersion":"1.0.4","platform":"mac-10.7 (Lion)-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO - 2013-09-09T18:17:12.531Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 0c5c9500-197c-11e3-8eed-b3b7a73965aa
一切似乎都很好,但代码崩溃了:/我得到一个巨大的错误堆栈跟踪,主要是,
selenium.common.exceptions.WebDriverException: Message: u'Error forwarding the new session new session request for webdriver should contain a location header with the session.'
如果你检查网格集线器的输出,
INFO: Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=phantomjs, maxInstances=1}
INFO: Available nodes: [host :http://127.0.0.1:8080 time out : 300000]
似乎还有一个问题。看起来,无论出于何种原因,网格集线器都没有从 PhantomJS 可执行文件返回任何信息,即使它似乎正在正确处理事情。
为了测试,我还在同一台机器上使用命令运行了一个常规的 selenium 节点
java -jar selenium-server-standalone-2.33.0.jar -role node
如果我启动一个 Firefox RemoteWebDriver session (在 Python 中),用
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX)
一切都很好。
看起来一切都设置正确,我将事情保持为“ Vanilla ”并尽可能接近文档所说的内容,但这里无法越过围栏。
在 Java 中尝试时也会发生完全相同的错误
WebDriver driver = new RemoteWebDriver(DesiredCapabilities.phantomjs());
两个连接的网格控制台的屏幕截图,PhantomJS 还活着,而且很好,
左边是默认的 selenium 节点,右边是 PhantomJS(10.0.0.222 是我笔记本电脑的工作 IP 地址)。
我的 PhantomJS 版本是 1.9.2,selenium 版本显然是 2.33.0。
最佳答案
原来这是phantomjs 1.9.2版本的一个bug特性。他们更新到最新的 ghostdriver (v1.4) 库,I imagine 就是它被引入的地方。 Ghostdriver 遵守新的 Selenium 协议(protocol),使用帖子创建 session ,而不是使用位置 header 。 Selenium 现在从帖子正文中的 json 中获取位置等。事实证明,您看到的错误和我一直看到的错误是因为您使用的是旧的 selenium jar,它使用旧式 session 创建协议(protocol)和新版本的 selenium。现在你的问题的答案是回到 phantomjs 1.9.1 将你的 selenium jar 更新到 2.35.0(见下面的链接)。为了重现您的错误,我测试了 ruby 和 python selenium webdrivers 以及作为 hub 运行的 2.33.0 selenium jar 和作为 ghostdriver worker 运行的 phantomjs 1.9.2。
这是 github 问题中描述的相同错误:
https://github.com/ariya/phantomjs/issues/11610
我相信它与这个特定的提交有关。您可以看到“位置”标题已被删除。
https://github.com/detro/ghostdriver/commit/60bf3c90aa825cc3db4fa899b6a67bb7f109d55d
这是对上述提交所针对的问题的描述: https://github.com/detro/ghostdriver/issues/247
从那里你可以看到它是一个让事情与 selenium-webdrivers 最新协议(protocol)一起工作的修复: “新规范:https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session Selenium 的变化:https://code.google.com/p/selenium/source/detail?r=8c3c65772d57 "
看起来也像 更新到最新的 selenium-webdriver-standalone jar 将使 1.9.2 正常工作。我已经确认我的 ruby webdriver 现在可以成功连接 2.35.0 jar 和 phantomjs 1.9.2。
您可以在这里获取最新的 jar:
关于python - 使用 Selenium RemoteWebDriver 运行 PhantomJS 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18704626/