Web驱动程序异常:错误提示:未知错误Chrome浏览器无法启动:使用ChromeDriver Chrome和Selenium Python时退出异常错误

vcudknz3  于 2023-02-10  发布在  Go
关注(0)|答案(3)|浏览(143)

我试图在我的虚拟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.)

有人已经有这个问题或有任何建议吗?

ekqde3dh

ekqde3dh1#

一个月一个月

A common cause for Chrome to crash during startup is running Chrome as **root** user (**administrator**) on Linux. While it is possible to work around this issue by passing--no-sandboxflag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.
第一条错误消息...

/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无法启动/生成新的浏览上下文,即Chrome浏览器会话。
根据Selenium的python客户端的Release Notesv3.8.0chrome_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')
disho6za

disho6za2#

您可以使用以下代码片段在服务器上运行seleniumcrawler(ubuntu中没有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')
z9ju0rcb

z9ju0rcb3#

我在谷歌上搜索Ruby错误,但发现了这个。对一些人来说可能会有帮助。
我错误地设置了水豚会话(它本身依赖于 selenium )

# WRONG
session = Capybara::Session.new(:selenium_chrome)

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

相关问题