在无头模式下使用 ChromeDriver 和 Chrome 以及 --disable-gpu 和 --user-data-dir 参数时,GPU 进程崩溃了 x 次错误

Cod*_*er7 6 python selenium selenium-chromedriver selenium-webdriver google-chrome-headless

我正在使用硒来打开一个网站。具体来说:

https://storage.googleapis.com/tfjs-models/demos/posenet/camera.html
Run Code Online (Sandbox Code Playgroud)

但是当它使用无头模式打开它时,它显示以下错误:

[0513/155639.694:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 1 time(s)

DevTools listening on ws://127.0.0.1:6573/devtools/browser/58f34b20-1a05-4bf0-8e64-1b5a5028b1f6
[0513/155639.710:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
[0513/155639.710:ERROR:cache_util.cc(138)] Unable to move cache folder C:/Users/Tazim Fatima/AppData/Local/Google/Chrome/User Data\Default\GPUCache to C:/Users/Tazim Fatima/AppData/Local/Google/Chrome/User Data\Default\old_GPUCache_000
[0513/155639.710:ERROR:disk_cache.cc(184)] Unable to create cache
[0513/155639.710:ERROR:shader_disk_cache.cc(606)] Shader Cache Creation failed: -2
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 2 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 3 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 4 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 5 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 6 time(s)
[0513/155639.725:ERROR:browser_gpu_channel_host_factory.cc(153)] Failed to launch GPU process.
[0513/155642.754:ERROR:service_worker_storage.cc(1624)] Failed to delete the database: Database IO error`enter code here`
Run Code Online (Sandbox Code Playgroud)

经过大量搜索后,我尝试了:

options.add_argument("--disable-user-media-security=true")
options.add_argument('--headless')
options.add_argument("--no-sandbox")  
options.add_argument("--use-fake-ui-for-media-stream")
options.add_argument("--disable-gpu")
options.add_argument("--disable-software-rasterizer")
options.add_argument('--disable-gpu')
options.add_argument("--window-size=1280,1024")
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--ignore-gpu-blacklist')
options.add_argument('--use-gl')
options.add_argument("--user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")

driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.set_window_size(1200, 600)
driver.set_window_size(1280, 1024)
driver.get("https://storage.googleapis.com/tfjs-models/demos/posenet/camera.html")
Run Code Online (Sandbox Code Playgroud)

当然,不是同时使用所有参数,而是在不同时间使用不同的块。虽然这里需要注意的一件事是,当我使用参数--no-sandbox时,错误变为

DevTools listening on ws://127.0.0.1:1849/devtools/browser/426a187c-243f-42e3-a0b7-60c4f8dc73b2
[0513/163007.734:ERROR:gles2_cmd_decoder.cc(3610)] ContextResult::kFatalFailure: fail_if_major_perf_caveat + swiftshader
[0513/163007.742:ERROR:gles2_cmd_decoder.cc(3610)] ContextResult::kFatalFailure: fail_if_major_perf_caveat + swiftshader
[0513/163007.754:ERROR:gles2_cmd_decoder.cc(3610)] ContextResult::kFatalFailure: fail_if_major_perf_caveat + swiftshader
[0513/163007.758:INFO:CONSOLE(20)] "Initialization of backend webgl failed", source: https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js (20)
[0513/163007.758:INFO:CONSOLE(20)] "Error: WebGL is not supported on this device
    at new e (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:294522)
    at Object.factory (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:346621)
    at t.initializeBackend (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:43098)
    at t.initializeBackendsAndReturnBest (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:44537)
    at t.get [as backend] (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:41137)
    at t.makeTensor (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:48217)
    at Tn (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:81890)
    at Dn (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:81021)
    at o (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:491592)
    at Object.Pp [as decodeWeights] (https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js:20:491626)", source: https://storage.googleapis.com/tfjs-models/demos/posenet/camera.cb338cd2.js (20)
Run Code Online (Sandbox Code Playgroud)

它有效。但与非无头模式相比,它非常非常慢。

而且我无法快速进行交易。我怎么解决这个问题?

Deb*_*anB 3

这个错误信息...

[0513/155639.710:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
[0513/155639.710:ERROR:cache_util.cc(138)] Unable to move cache folder C:/Users/Tazim Fatima/AppData/Local/Google/Chrome/User Data\Default\GPUCache to C:/Users/Tazim Fatima/AppData/Local/Google/Chrome/User Data\Default\old_GPUCache_000
[0513/155639.710:ERROR:disk_cache.cc(184)] Unable to create cache
[0513/155639.710:ERROR:shader_disk_cache.cc(606)] Shader Cache Creation failed: -2
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 2 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 3 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 4 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 5 time(s)
[0513/155639.725:WARNING:gpu_process_host.cc(1204)] The GPU process has crashed 6 time(s)
[0513/155639.725:ERROR:browser_gpu_channel_host_factory.cc(153)] Failed to launch GPU process.
[0513/155642.754:ERROR:service_worker_storage.cc(1624)] Failed to delete the database: Database IO error
Run Code Online (Sandbox Code Playgroud)

...意味着 ChromeDriver/Chrome 进程无法将缓存文件夹移动..\Default\GPUCache../AppData/Local/Google/Chrome/User Data\Default\old_GPUCache_000.


深潜

根据文章《Headless: make --disable-gpu flag不必要》,许多文章、文档和指南建议使用该--disable-gpu标志,因为该标志libosmesa.so并不存在。随着无头合并功能的推出:从 osmesa 切换到 SwiftShader,在 Linux 或 macOS 上不再需要此标志。随着合并的可用性始终使用 SwiftShader for headless Chrome,即使在 Windows 上,此标志也是不必要的。


解决方案

作为解决方案,您需要删除以下参数:

  • --disable-gpu
  • --disable-software-rasterizer

您可以在以下位置找到一些相关讨论:

此外,当尝试访问特定的Chrome 配置文件而不是提及顶级User Data目录时,您需要更深入地提及配置文件目录,如下../User Data/Profile 2所示:

options.add_argument("--user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Profile 2")
Run Code Online (Sandbox Code Playgroud)

您可以在以下位置找到一些相关讨论: