Python 使用Selenium RemoteWebDriver运行PhantomJS时出错

Python 使用Selenium RemoteWebDriver运行PhantomJS时出错,python,selenium,selenium-webdriver,phantomjs,selenium-grid,Python,Selenium,Selenium Webdriver,Phantomjs,Selenium Grid,我使用selenium grid hub的默认启动命令启动它 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-09

我使用selenium grid hub的默认启动命令启动它

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
一切似乎都很好,但代码崩溃了:/I我得到一个巨大的错误stacktrace,主要是

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]
这似乎也是一个问题。不管出于什么原因,grid hub似乎没有从PhantomJS可执行文件中获取任何信息,尽管它似乎正在正确地处理事情

为了进行测试,我还使用命令在同一台机器上运行了一个常规的selenium节点

java -jar selenium-server-standalone-2.33.0.jar -role node
如果我启动Firefox RemoteWebDriver会话(用Python),则

一切都很好

看起来一切都设置正确了,我把事情保持在“普通”状态,尽可能接近医生所说的,但就是无法越过这道篱笆

在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)库,我想这就是它被引入的地方。Ghostdriver遵守新的Selenium协议,使用POST创建会话,而不使用位置头。Selenium现在从文章正文中的json中获取位置等。事实证明,您看到的错误和我看到的错误是因为您使用的是旧的selenium jar,它在新版本的selenium中使用了旧式会话创建协议。现在,您的问题的答案是返回到phantomjs 1.9.1,将selenium jar更新为2.35.0(参见下面的链接)。为了重现您的错误,我测试了ruby和python selenium webdrivers以及作为hub运行的2.33.0 selenium jar和作为GhostDrivers工作者运行的phantomjs 1.9.2

以下是github问题中描述的相同错误:

我相信这与这个特殊的承诺有关。您可以看到“位置”的标题已被删除

以下是上述提交所针对的问题的描述:

从这一点可以看出,这是一个解决方案,可以让selenium webdrivers的最新协议发挥作用: “新规格: 硒的变化:

看起来更新到最新的selenium webdriver standalone jar将允许您使用1.9.2。我已经确认我的ruby webdriver现在可以成功地连接到2.35.0JAR和PhantomJS1.9.2

您可以在此处获得最新的jar:


当您打开
http://127.0.0.1:4444
在浏览器上,您是否看到您的phantomjs节点已连接?是的“WebDriver phantomjs”。但当我转到配置选项卡时,主机对于phantomjs是“null”。屏幕截图:我已经用phantomjs 1.9.2+selenium 2.35.0和phantomjs 1.9.1+selenium 2.33.0测试了这两个版本,但仍然出现这个错误。我使用i686版本的phantomjs,因为我有一个x86 ubuntu。你还有其他想法吗?有趣的是,如果我在同一台机器上运行Gradutor、selenium standalone server 2.35.0和phantomjs 1.9.2,它会工作,但如果我在远程机器上运行Gradutor,selenium会挂起“尝试在测试槽上创建新会话”。
WebDriver driver = new RemoteWebDriver(DesiredCapabilities.phantomjs());