Python Selenium无法连接到Webdriver Firefox扩展

时间:2013-09-25 06:19:47

标签: python firefox selenium

我使用的是Python 2.7,Selenium 2.35和Firefox 22.0。当我这样做时:

from selenium import webdriver

d = webdriver.Firefox()

Firefox浏览器打开并保持打开状态。控制台中会抛出以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 61, in __init__
    desired_capabilities=capabilities)
  File "c:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 72, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "c:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 114, in start_session
    'desiredCapabilities': desired_capabilities,
  File "c:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "c:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 136, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: u''

然后我启动了Fiddler并尝试再次初始化webdriver.Firefox对象:

d = wedriver.Firefox()

与上述相同的错误。 Fiddler显示以下输出:

{"name":"newSession","status":13,"value":{"message":"b is undefined","stackTrace":[{"methodName":"wdSessionStoreService.prototype.extractCapabilitySetting_",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/session_store.js",
"lineNumber":7951},{"methodName":"wdSessionStoreService.prototype.createSession",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/session_store.js",
"lineNumber":7944},{"methodName":"nsCommandProcessor.prototype.newSession",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/command_processor.js",
"lineNumber":10982},{"methodName":"nsCommandProcessor.prototype.execute",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/command_processor.js",
"lineNumber":10875},{"methodName":"Dispatcher.executeAs/<",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/driver_component.js",
"lineNumber":7750},{"methodName":"Resource.prototype.handle",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/driver_component.js",
"lineNumber":7896},{"methodName":"Dispatcher.prototype.dispatch",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/driver_component.js","lineNumber":7843},{"methodName":"WebDriverServer/<.handle","fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/driver_component.js","lineNumber":10710},{"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/httpd.js","lineNumber":1935},{"methodName":"ServerHandler.prototype.handleResponse","fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/httpd.js","lineNumber":2261},{"methodName":"Connection.prototype.process","fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/httpd.js","lineNumber":1168},{"methodName":"RequestReader.prototype._handleResponse","fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/httpd.js","lineNumber":1616},{"methodName":"RequestReader.prototype._processBody","fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/httpd.js","lineNumber":1464},{"methodName":"RequestReader.prototype.onInputStreamReady",
"fileName":"file:///c:/users/username/appdata/local/temp/tmpzqvpr1/extensions/fxdriver@googlecode.com/components/httpd.js","lineNumber":1333}]}}

如果我加载localhost:57554,我会:

httpd.js

If you're seeing this page, httpd.js is up and serving requests! Now set a base path and serve some files!

因此Webdriver似乎正常运行,但由于某些原因,Python无法启动会话。 以前有人遇到过这个问题吗?什么可能导致这个?

1 个答案:

答案 0 :(得分:0)

此问题是由HTTP_PROXY环境变量引起的。通过“控制面板”更改Windows设置不会更改环境变量的值。在实例化webdriver之前从Python环境中删除变量可以解决问题:

import os

if 'HTTP_PROXY' in os.environ:
    os.environ.pop('HTTP_PROXY')