WebDriverException:消息:未知错误:Chrome启动失败:使用ChromeDriver Chrome和Selenium Python异常退出错误

WebDriverException:消息:未知错误:Chrome启动失败:使用ChromeDriver Chrome和Selenium Python异常退出错误,python,selenium,google-chrome,selenium-webdriver,selenium-chromedriver,Python,Selenium,Google Chrome,Selenium Webdriver,Selenium Chromedriver,我正试图在我的虚拟ubuntu外壳上安装和使用Selenium chromedriver,我一步一步地学习各种教程,但似乎仍然有一些地方出了问题。。。在对这个问题进行了很多研究之后,我找不到任何答案 下面是我正在尝试运行的小代码: from selenium import webdriver from selenium.webdriver import Chrome from selenium.webdriver.chrome.options import Options chrome_opt

我正试图在我的虚拟ubuntu外壳上安装和使用Selenium chromedriver,我一步一步地学习各种教程,但似乎仍然有一些地方出了问题。。。在对这个问题进行了很多研究之后,我找不到任何答案

下面是我正在尝试运行的小代码:

from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')
我添加了chrome_选项,这要感谢我在各种主题上找到的建议。但是我真的不明白为什么有必要

不幸的是,我的程序向我发送了以下错误,我被卡住了,不知道该怎么办:

/home/lclis/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:7: DeprecationWarning: use options instead of chrome_options
  import sys
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-4-259faa721232> in <module>
      5 chrome_options.add_argument('--no-sandbox')
      6 chrome_options.add_argument('--disable-extensions')
----> 7 driver = webdriver.Chrome(chrome_options=chrome_options)
      8 driver.get('https://google.com')

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     79                     remote_server_addr=self.service.service_url,
     80                     keep_alive=keep_alive),
---> 81                 desired_capabilities=desired_capabilities)
     82         except Exception:
     83             self.quit()

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    155             warnings.warn("Please use FirefoxOptions to set browser profile",
    156                           DeprecationWarning, stacklevel=2)
--> 157         self.start_session(capabilities, browser_profile)
    158         self._switch_to = SwitchTo(self)
    159         self._mobile = Mobile(self)

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    250         parameters = {"capabilities": w3c_caps,
    251                       "desiredCapabilities": capabilities}
--> 252         response = self.execute(Command.NEW_SESSION, parameters)
    253         if 'sessionId' not in response:
    254             response = response['value']

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
有人已经有过这个问题或对此有什么建议吗?

经验法则 Chrome在启动期间崩溃的一个常见原因是在Linux上以root用户管理员身份运行Chrome。虽然在创建WebDriver会话时可以通过传递-no-sandbox标志来解决此问题,但这种配置不受支持,并且非常不推荐。您需要将您的环境配置为以普通用户身份运行Chrome

这是第一条错误消息

/home/lclis/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:7: DeprecationWarning: use options instead of chrome_options
  import sys
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-4-259faa721232> in <module>
      5 chrome_options.add_argument('--no-sandbox')
      6 chrome_options.add_argument('--disable-extensions')
----> 7 driver = webdriver.Chrome(chrome_options=chrome_options)
您可以在ubuntu服务器上运行selenium crawler,该服务器上没有GUI,没有可用的X窗口,并带有以下代码段:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')

driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver', options=chrome_options)
driver.get('https://google.com')

我在谷歌上搜索ruby错误,但发现了这个。对一些人来说,这可能是有帮助的

我错误地设置了依靠硒的水豚会话

错误的 session=Capybara::session.new:selenium\u chrome 正确,请参见添加的headless session=Capybara::session.new:selenium\u chrome\u headless
这回答了你的问题吗?你找到解决这个问题的办法了吗?我在Ubuntu20VPS中遇到了同样的问题,到目前为止,我唯一得到的是,由于Ubuntu20VPS没有安装GUI,除了通过无头论证之外,它不能使用chromedriver。谢谢你的反馈!我按照您的建议更改了参数,但同样的错误正在发生。我的操作系统是windows,但我在windows商店用Ubuntu操作jupyter笔记本,我下载了linux chromedriver。你认为这能解释这个错误吗?@LouisClisson如果底层操作系统是Ubuntu,我想你是对的。你能签出更新的答案并告诉我状态吗。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')

driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver', options=chrome_options)
driver.get('https://google.com')