Python中Selenium ChromeDriver启动崩溃问题求助
Selenium ChromeDriver启动崩溃问题求助
我用Python的Selenium ChromeDriver时,每次运行代码都会崩溃,完全不知道怎么处理。
我的代码:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver=webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.google.com")
运行后输出的错误信息:
DevTools listening on ws://127.0.0.1:58441/devtools/browser/a6122e07-0d3e-49ea-9308-d5c4dc4461ab [19592:21540:0929/222340.633:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.664:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.671:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.686:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.693:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.695:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.698:ERROR:device_event_log_impl.cc(214)] [22:23:40.698] USB: usb_service_win.cc:415 Could not read device interface GUIDs: 系统找不到指定的文件。(0x2) [19592:21540:0929/222340.698:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.700:ERROR:device_event_log_impl.cc(214)] [22:23:40.700] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: 连接到系统的某个设备不能正常工作。(0x1F) [19592:21540:0929/222340.705:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.712:ERROR:gpu_process_host.cc(968)] GPU process launch failed: error_code=2 [19592:21540:0929/222340.712:FATAL:gpu_data_manager_impl_private.cc(441)] GPU process isn't usable. Goodbye.
崩溃回溯信息:
Traceback (most recent call last): File "c:\Users\marti\OneDrive\Escritorio\woo\DiscordBot\BetterCollege\tester.py", line 5, in <module> driver=webdriver.Chrome(service=Service(ChromeDriverManager().install())) File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__ super().__init__(DesiredCapabilities.CHROME['browserName'], "goog", File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 92, in __init__ super().__init__( File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 272, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 364, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute self.error_handler.check_response(response) File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: 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.) Stacktrace: Backtrace: Ordinal0 [0x00EB1ED3+2236115] Ordinal0 [0x00E492F1+1807089] Ordinal0 [0x00D566FD+812797] Ordinal0 [0x00D758F9+940281] Ordinal0 [0x00D719C9+924105] Ordinal0 [0x00DA806C+1146988] Ordinal0 [0x00DA7A6A+1145450] Ordinal0 [0x00DA18A6+1120422] Ordinal0 [0x00D7A73D+960317] Ordinal0 [0x00D7B71F+964383] GetHandleVerifier [0x0115E7E2+2743074] GetHandleVerifier [0x011508D4+2685972] GetHandleVerifier [0x00F42BAA+532202] GetHandleVerifier [0x00F41990+527568] Ordinal0 [0x00E5080C+1837068] Ordinal0 [0x00E54CD8+1854680] Ordinal0 [0x00E54DC5+1854917] Ordinal0 [0x00E5ED64+1895780] BaseThreadInitThunk [0x768F6739+25] RtlGetFullPathName_UEx [0x77658FD2+1218] RtlGetFullPathName_UEx [0x77658F9D+1165]
恳请各位帮忙解决这个问题!
解决方案
从错误日志看,核心问题是GPU进程启动失败导致Chrome崩溃,试试以下几种方法:
禁用GPU加速
在ChromeOptions中添加--disable-gpu参数,这是解决GPU相关崩溃最直接的方法:from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--disable-gpu") driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver.get("https://www.google.com")添加更多稳定参数
可以同时添加几个常用的稳定参数,进一步避免崩溃:chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--remote-debugging-port=9222")检查Chrome与ChromeDriver版本匹配
虽然用了webdriver-manager自动管理版本,但偶尔会出现版本不兼容的情况。手动打开Chrome,在chrome://settings/help查看当前Chrome版本,然后指定对应版本的ChromeDriver:# 示例:指定ChromeDriver版本为118.0.5993.70 driver = webdriver.Chrome(service=Service(ChromeDriverManager(version="118.0.5993.70").install()), options=chrome_options)关闭所有Chrome进程再运行
打开任务管理器,结束所有chrome.exe进程,然后重新运行脚本。以管理员身份运行脚本
右键点击Python脚本,选择“以管理员身份运行”,避免权限不足导致的启动失败。
内容的提问来源于stack exchange,提问作者empress
相关产品推荐
相关产品推荐

