【问题标题】:WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally error using ChromeDriver Chrome and Selenium PythonWebDriverException:消息:未知错误:Chrome 无法启动:使用 ChromeDriver Chrome 和 Selenium Python 异常退出错误
【发布时间】:2020-01-30 16:38:09
【问题描述】:

我正在尝试在我的虚拟 ubuntu shell 上安装和使用 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_options。但是我真的不明白为什么有必要?

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

/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.)

有人已经遇到过这个问题或有什么建议吗?

【问题讨论】:

标签: python selenium google-chrome selenium-webdriver selenium-chromedriver


【解决方案1】:

您可以使用此代码在服务器上运行 selenium 爬虫(带有 无 GUI,框内没有 X 窗口 可用的 ubuntu) sn-p:

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')

【讨论】:

    【解决方案2】:

    拇指规则

    Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 运行 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)
    

    ...暗示 ChromeDriver 无法启动/产生新的 Browsing ContextChrome Browser 会话。


    根据Selenium 客户端的Release Notes chrome_options 已弃用:

    浏览器选项参数现在跨驱动程序标准化为optionsfirefox_optionschrome_optionsie_options 现已弃用。


    解决方案

    在初始化 ChromeDriver/Chrome 会话时,您需要使用 options 而不是 chrome_options。因此,您的代码块将是:

    from selenium import webdriver
    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(executable_path='/path/to/chromedriver', options=chrome_options)
    driver.get('https://google.com')
    

    【讨论】:

    • 您好,感谢您的反馈!我按照您的建议更改了参数,但发生了同样的错误。我的操作系统是 Windows,但我在 Windows Store 上使用 Ubuntu 使用 jupyter notebook,我下载了 linux chromedriver。你认为它可以解释这个错误吗?
    • @LouisClisson 如果底层操作系统是 Ubuntu,我猜你是在正确的轨道上。您能否查看更新的答案并让我知道状态。
    • “您需要配置您的环境” - 任何链接或提示如何正确设置它?
    【解决方案3】:

    我在谷歌上搜索 ruby​​ 错误,但发现了这个。对于某些人来说,它可能会有所帮助。

    我错误地设置了 capybara 会话(它本身依赖于 selenium)

    # WRONG
    session = Capybara::Session.new(:selenium_chrome)
    
    # CORRECT, see headless added
    session = Capybara::Session.new(:selenium_chrome_headless)
    

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 2017-09-07
      • 1970-01-01
      • 2020-02-05
      • 2020-05-21
      相关资源
      最近更新 更多