Python Selenium运行错误的浏览器(默认)

Python Selenium运行错误的浏览器(默认),python,firefox,selenium,Python,Firefox,Selenium,tl/dr:我做错了什么 我试图在本地运行selenium测试,并与Browserstack平台兼容。我使用此代码进行本地连接: wd = webdriver.Remote('http://xxxxxxxx@hub.browserstack.com:80/wd/hub', {'browser':'firefox'}) wd.get('http://google.com') wd.get_screenshot_as_file('/tmp/googl.png') wd.close() 我在/tmp

tl/dr:我做错了什么

我试图在本地运行selenium测试,并与Browserstack平台兼容。我使用此代码进行本地连接:

wd = webdriver.Remote('http://xxxxxxxx@hub.browserstack.com:80/wd/hub', {'browser':'firefox'})
wd.get('http://google.com')
wd.get_screenshot_as_file('/tmp/googl.png')
wd.close()
我在
/tmp/
中看到了一个很好的屏幕截图

现在,我尝试对本地Selenium执行相同的操作:

$ java -jar /usr/share/java/selenium-server-standalone-2.44.0.jar &
服务器名义上启动。我尝试用Firefox(30.0)创建一个会话,它工作正常。(默认浏览器为Opera。)

然后我尝试运行Python代码:

wd = webdriver.Remote('http://localhost:4444/wd/hub', {'browser':'firefox'})
wd.get('http://google.com')
wd.get_screenshot_as_file('/tmp/googl2.png')
wd.close()
Selenium打开Opera而不是Firefox。

我在Python控制台中看到了这一点:

Message: <html>
<head>
<title>Error 500 org/json/JSONObject</title>
</head>
<body>
<h2>HTTP ERROR: 500</h2><pre>org/json/JSONObject</pre>
<p>RequestURI=/wd/hub/session</p>
<p><i><small><a href="http://jetty.mortbay.org">Powered by Jetty://</a></small></i></p>
消息:
错误500 org/json/JSONObject
HTTP错误:500org/json/JSONObject
RequestURI=/wd/hub/session


为什么它要打开Opera而不是Firefox?

问题在于:

wd = webdriver.Remote('http://localhost:4444/wd/hub', {'browserName':'firefox'})
browser
更改为
browserName
将修复此问题。使用

from selenium import webdriver

capabilities = webdriver.DesiredCapabilities.FIREFOX.copy()
wd = webdriver.Remote('http://localhost:4444/wd/hub', desired_capabilities=capabilities)
相反。

另一个解决方案(非常接近公认的答案)是使用预定义的所需功能:


在本例中,
capabilities
是一个dict,它已经包含
browserName
属性,设置为
firefox

,同样,它会启动Opera,尝试在调试模式下工作。编辑oops。没关系,它成功了。谢谢伟大的很高兴这有帮助。
from selenium import webdriver

capabilities = webdriver.DesiredCapabilities.FIREFOX.copy()
wd = webdriver.Remote('http://localhost:4444/wd/hub', desired_capabilities=capabilities)