python - Webdriver 异常 :Process unexpectedly closed with status: 1

标签 python selenium

我尝试在 Linux 机器上运行一个 selenium 程序。但是我遇到了异常:

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 154, in __init__
    keep_alive=True)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
     File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 1

如何修复异常?感谢您的帮助。

最佳答案

当您尝试在没有显示器的盒子(如 Ubuntu 服务器)上以非 headless 模式运行浏览器时,可能会出现此错误。

您可以通过查看通常留在您的工作目录中的 geckodriver.log 文件来检查这是否是您的 Process unexpectedly closed with status: 1 错误的原因运行脚本后,它应该有一行:

Error: GDK_BACKEND does not match available displays

如果您在 geckodriver.log 中看到该行,那么您需要切换脚本以在 headless 模式下运行 Firefox:

from selenium import webdriver
from selenium.webdriver import FirefoxOptions

opts = FirefoxOptions()
opts.add_argument("--headless")
browser = webdriver.Firefox(options=opts)

browser.get('http://example.com')

关于python - Webdriver 异常 :Process unexpectedly closed with status: 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46809135/

相关文章:

selenium - 如何告诉 Webdriver 使用特定版本的 ChromeDriver?

selenium - 如何使用 Java 在 Selenium Webdriver 中使用 clickandwait?

python - 如何使用python在selenium中通过部分id名称查找元素

python - 为什么我应该在 python 的多线程中在 start() 之后加入()?

firefox - 哪个 Firefox 版本与 Selenium 2.48.0 兼容

python - Windows XP GTK 应用程序 Glib 导入错误

python - 用于部署单节点的 Storm 替代方案

node.js - 离线运行 web-component-tester

python - 根据条件替换 Pandas Dataframe 中的值

python - 了解 python 中的嵌套 lambda 函数行为