如何防止Chrome浏览器因Selenium而崩溃?

zwghvu4y  于 2023-01-28  发布在  Go
关注(0)|答案(1)|浏览(634)

bounty将在7天后过期。回答此问题可获得+250声望奖励。Graeme Perrow希望引起更多人关注此问题。

我正在使用node.js中的Selenium在Windows 11上的Chrome中测试网页。我最近换了一台新笔记本电脑,现在我的Chrome浏览器在驱动程序启动时就会崩溃。我使用的是Chrome和Chromedriver版本109.0.5414.74(与旧机器上的相同)、node.js v16.13.0和selenium-webdriver v4.7.1。除了Selenium之外,Chrome运行良好。下面是简单的演示:

const { Builder } = require( 'selenium-webdriver' );
const chrome = require( 'selenium-webdriver/chrome' );

async function main() {
    const chrome_options = new chrome.Options()
    .addArguments( '--disable-gpu' );
    const driver = await new Builder()
    .forBrowser( 'chrome' )
    .setChromeOptions( chrome_options )
    .build();

    await driver.get( 'https://google.com/' );

    await driver.quit();
}

main()
.then( () => {
    console.log( 'Done' );
})
.catch( e => {
    console.error( `Error: ${e.message}` );
});

我之所以添加disable-gpu,是因为一些google搜索结果显示需要它,而且错误消息提到了GPU问题,但没有它也没有什么不同。

DevTools listening on ws://127.0.0.1:53101/devtools/browser/701cedaa-d8e3-4541-9c9b-d22f53244ac4
[29204:3600:0124/223239.948:ERROR:display_layout.cc(551)] PlacementList must be sorted by first 8 bits of display_id
[29204:3600:0124/223240.003:ERROR:gpu_process_host.cc(985)] GPU process launch failed: error_code=18
[29204:3600:0124/223240.038:ERROR:display_layout.cc(551)] PlacementList must be sorted by first 8 bits of display_id
[29204:3600:0124/223240.177:ERROR:device_event_log_impl.cc(215)] [22:32:40.176] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2)
[29204:3600:0124/223240.179:ERROR:device_event_log_impl.cc(215)] [22:32:40.178] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.179:ERROR:usb_descriptors.cc(141)] Failed to read length for configuration 1.
[29204:3600:0124/223240.180:ERROR:usb_descriptors.cc(100)] Failed to read all configuration descriptors. Expected 2, got 1.
[29204:3600:0124/223240.181:ERROR:device_event_log_impl.cc(215)] [22:32:40.180] USB: usb_device_win.cc:96 Failed to read descriptors from \\?\usb#vid_17ef&pid_3082#101b74e97#{a5dcbf10-6530-11d2-901f-00c04fb951ed}.
[29204:3600:0124/223240.181:ERROR:device_event_log_impl.cc(215)] [22:32:40.181] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.190:ERROR:device_event_log_impl.cc(215)] [22:32:40.190] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.191:ERROR:device_event_log_impl.cc(215)] [22:32:40.192] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.191:ERROR:device_event_log_impl.cc(215)] [22:32:40.193] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.222:ERROR:device_event_log_impl.cc(215)] [22:32:40.232] Bluetooth: bluetooth_adapter_winrt.cc:1164 RequestRadioAccessAsync failed: RadioAccessStatus::DeniedByUserWill not be able to change radio power.
[29204:3600:0124/223240.238:ERROR:device_event_log_impl.cc(215)] [22:32:40.240] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.238:ERROR:device_event_log_impl.cc(215)] [22:32:40.247] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[29204:3600:0124/223240.238:ERROR:gpu_process_host.cc(985)] GPU process launch failed: error_code=18
[29204:3600:0124/223240.457:ERROR:gpu_process_host.cc(985)] GPU process launch failed: error_code=18
[29204:3600:0124/223240.598:ERROR:gpu_process_host.cc(985)] GPU process launch failed: error_code=18
[29204:3600:0124/223240.739:ERROR:gpu_process_host.cc(985)] GPU process launch failed: error_code=18
[29204:3600:0124/223240.865:ERROR:gpu_process_host.cc(985)] GPU process launch failed: error_code=18
[29204:3600:0124/223240.881:FATAL:gpu_data_manager_impl_private.cc(440)] GPU process isn't usable. Goodbye.
Error: 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.)

我怎么知道为什么会崩溃?

pn9klfpd

pn9klfpd1#

此错误消息表示GPU进程启动有问题。error_code=18表示GPU进程已崩溃,这也导致Chrome浏览器崩溃。这可能是由于Chrome版本与新笔记本电脑上的GPU驱动程序之间的兼容性问题。
您可以尝试更新新笔记本电脑上的GPU驱动程序,看看是否可以解决此问题。
或者,您可以尝试使用其他版本的ChromeChromedriver**(已知与新笔记本电脑上的GPU驱动程序兼容)。您还应检查新笔记本电脑上运行的任何其他软件是否与SeleniumChrome**冲突。
另外,尝试使用--no-sandbox选项运行脚本,看看它是否有效。

const chrome_options = new chrome.Options()
  .addArguments("--disable-gpu")
  .addArguments("--no-sandbox");

相关问题