解决Selenium使用时报错Chrome not reachable问题

最近在写一个爬虫小工具,但是在第一步就遇到了问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.edge.options import Options
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=9222')
options.binary_location = r"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
options.add_argument('user-data-dir=C:\\Users\\Jackie_Li\\AppData\\Local\\Microsoft\\Edge\\User Data')

service = Service(r'E:\Webdrivers\chromedriver.exe')

driver = webdriver.Chrome(options=options,service=service)

报错信息为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Traceback (most recent call last):
File "c:\Users\Jackie_Li\Desktop\爬虫.py", line 19, in <module>
driver = webdriver.Chrome(options=options,service=service)
File "C:\Users\Jackie_Li\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
super().__init__(
File "C:\Users\Jackie_Li\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
File "C:\Users\Jackie_Li\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 212, in __init__
self.start_session(capabilities)
File "C:\Users\Jackie_Li\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 299, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "C:\Users\Jackie_Li\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 354, in execute
self.error_handler.check_response(response)
File "C:\Users\Jackie_Li\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: crashed.
(chrome not reachable)
(The process started from chrome location C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

在解决问题之前,我已经将所有在论坛上找到的可能的解决办法加入到了上面那段代码中,其中包括:

  • 添加参数--headless,--no-sandbox,--disable-dev-shm-usage,--remote-debugging-port=<port>
  • 添加chrome浏览器的可执行文件路径
  • 添加用户信息路径
  • 使用管理员/非管理员权限执行代码

但是这些方法都没有解决问题。

直到搜索到了知乎上的一篇文章:chrome not reachable ——使用undetected_chromedriver出现以上错误如何解决? - 知乎 (zhihu.com)

这篇文章的作者也遇到了同样的问题,而github上的一个开源项目作者MattWaller认为有可能是有可能是上面的用户信息路径中的某些文件在相关的进程关闭的时候被加了锁导致现在新开的ChromeDriver没法正常使用这些文件(但是MattWaller也没有指出具体的文件)。但是作者认为可以尝试更换一个新的用户信息路径,并且尝试在每次退出的时候运行:

1
2
driver.quit()
driver.close()

这个方法很管用!我们直接将用户信息更换为全新的路径即可:

1
2
options.add_argument("profile-directory=Profile 1");
options.add_argument('--user-data-dir=C:\\Users\\Jackie_Li\\PythonCreeper\\Profile 1');