如何修复WebDriverException:在我们连接之前,浏览器似乎已经退出了?

lau*_*ond 4 python selenium python-2.7

在我的Linux系统中,我使用Firefox,执行我的程序,我的错误是:

Traceback (most recent call last):

File "shenma_diff_main_v2.py", line 90, in <module>
    browser = webdriver.Firefox(profile)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 66, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
    raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
Run Code Online (Sandbox Code Playgroud)

如果我使用root执行我的程序就可以了.

小智 10

这是因为您没有正确设置以在没有GUI的情况下运行Firefox.这是一个可能对Ubuntu中的Selenium Headless Automated Testing有帮助的教程

我认为原因是你需要指定端口号,可能xvfb运行在一个终端
xvfb :99 -ac
上不同于Firefox的端口上:在另一个终端上:
export DISPLAY=:99
运行你的scrapy程序我没有使用root就成功了