ChromeDriver认为Chrome浏览器已崩溃,因为它启动的进程/usr/bin/google-chrome已经停止运行。

3

我正在Ubuntu服务器上运行Jupyter Notebook。我尝试使用ChromeDriver运行以下Selenium代码。但是,我遇到了下面的错误。如果我在Ubuntu终端中运行下面的代码行,它会返回下面的输出。有人知道问题可能是什么吗?我对Ubuntu还不太熟悉。

Ubuntu代码:

which google-chrome

输出:

/usr/bin/google-chrome

Jupyter Notebook 代码:

driver = webdriver.Chrome(executable_path=os.path.abspath("/home/username/chromedriver"), chrome_options=chrome_options)

错误:

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-5-bcd3289fe282> in <module>
     16 options.add_argument('headless') #downlod Chrome driver.exe
     17 #driver = webdriver.Chrome(executable_path=os.path.abspath("/home/username/stuff/JobHuntCode/chromedriver"), chrome_options=chrome_options)
---> 18 driver = webdriver.Chrome(executable_path=os.path.abspath("/home/username/chromedriver"), chrome_options=chrome_options)
     19 
     20 

~/anaconda3/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-46-generic x86_64)

更新:

更新:

Ubuntu

代码:

google-chrome --version

输出:

Google Chrome 73.0.3683.103

错误:

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-1-4c82dc01ab87> in <module>
     18 options.add_argument('headless') #downlod Chrome driver.exe
     19 #driver = webdriver.Chrome(executable_path=os.path.abspath("/home/username/stuff/JobHuntCode/chromedriver"), chrome_options=chrome_options)
---> 20 driver = webdriver.Chrome(executable_path=os.path.abspath("/home/username/chromedriver"), chrome_options=chrome_options)
     21 
     22 

~/anaconda3/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/envs/py36/lib/python3.6/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/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Linux 4.15.0-46-generic x86_64)

更新:

chrome_options.add_argument("no-sandbox")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")

由于某种原因,在我的代码中添加了以上这些行后,问题得以解决。我在下面的文章中找到了这段代码。

在ubuntu上执行Selenium UI测试用例时出现“未知错误:DevToolsActivePort文件不存在”的错误

1个回答

0

这个错误信息...

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/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-46-generic x86_64)

这意味着ChromeDriver无法启动/生成新的WebBrowser,即Chrome浏览器会话。

您的主要问题在于以下二进制文件版本之间的不兼容性

支持Chrome v74

  • 也许您正在使用最后一个GAed版本的Chrome浏览器,即chrome=73.x

因此,ChromeDriver v74.0.3729.6Chrome浏览器v73.x之间存在明显的不匹配。

解决方案

  • ChromeDriver 降级至 ChromeDriver v73.0.3683.68 版本。
  • 保持 Chrome 版本在 Chrome v73 级别。(根据 ChromeDriver v73.0.3683.68 发布说明
  • 通过你的 IDE 清理你的 项目工作区,并仅使用所需的依赖项重新构建你的项目。
  • 执行你的 @Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb 客户端实例。

谢谢您回复我。我下载了chromedriver的版本,现在我得到了相同的错误,但是使用新版本的chromedriver。我在原始帖子中添加了一个更新,其中包含错误信息。我还检查了正在运行的google-chrome版本为73.0.3683.103。(我也在更新中包括了它。)这是否意味着我必须获取chrome-driver 73.0.3683.103? - user3476463
我添加了一些在相关帖子中找到的解决问题的代码。我更新了代码并附上了帖子链接。 - user3476463
@user3476463,我本可以在答案中提供其他建议,但我尝试根据你的问题提供一个规范的答案。很高兴听到你的问题得到了解决。祝你好运。 - undetected Selenium
谢谢你的所有帮助。你能告诉我这三行代码的作用吗? "chrome_options.add_argument("no-sandbox") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--headless")" - user3476463
@user3476463 先生,我已经在我的答案中提供了所有的解释,就像这个问题中链接的那样,以 addArguments("--no-sandbox"); // Bypass OS security modeladdArguments("--disable-extensions"); // disabling extensions 的形式。如果您需要进一步的帮助,请告诉我。 - undetected Selenium

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接