【问题标题】:How to fix WebDriverException: The browser appears to have exited before we could connect? [duplicate]如何修复 WebDriverException:在我们连接之前浏览器似乎已经退出? [复制]
【发布时间】:2025-11-26 08:20:03
【问题描述】:

在我的 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.

如果我使用 root 执行我的程序是好的。

【问题讨论】:

  • 看我这里的答案*.com/questions/26070834/…>

标签: python python-2.7 selenium


【解决方案1】:

这是因为您没有正确设置在没有 GUI 的情况下运行 Firefox。 这是一个可能有用的教程 Selenium Headless Automated Testing in Ubuntu

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

【讨论】:

  • 链接的答案中没有提到您的评论,但这对我有用!
  • 谢谢,参考很清楚...