无法从节点连接读取描述符:在 Windows OS 上使用 ChromeDriver Selenium 时,连接到系统的设备无法运行错误

Failed to read descriptor from node connection: A device attached to the system is not functioning error using ChromeDriver Selenium on Windows OS

我在 运行 python 中的 selenium webdriver 脚本时得到了这个我还在系统环境中设置了路径,还尝试下载与我的 chrome 版本匹配的 webdriver。还有最简单的版本。但我仍然收到此错误:

[8552:6856:1120/155118.770:ERROR:device_event_log_impl.cc(211)] [15:51:18.771] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[8552:6856:1120/155118.774:ERROR:device_event_log_impl.cc(211)] [15:51:18.774] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[8552:6856:1120/155118.821:ERROR:device_event_log_impl.cc(211)] [15:51:18.821] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

我在我的代码中使用了它:

driver = webdriver.Chrome(resource_path("C:\webdriver\chromedriver.exe"))  # to open the chromebrowser
driver.get("https://web.whatsapp.com")

错误可能是因为您在 resource_path 变量中使用了括号。 代码应如下所示:

driver = webdriver.Chrome(resource_path="C:\webdriver\chromedriver.exe") # to open the chromebrowser 
driver.get("https://web.whatsapp.com")

如果仍然有任何问题,您可以尝试将网络驱动程序保存在与 python 文件相同的文件夹中。

这个错误信息...

[14432:11656:1120/161059.539:ERROR:device_event_log_impl.cc(211)] [16:10:59.539] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

...暗示 ChromeDriver 在尝试 initiate/spawn 新的 Browsing ContextChrome 浏览器 session.


分析

此错误是由于连接到 系统的 USB 设备无法正常运行所致。

此错误在 usb_device_handle_win.cc 中定义如下:

void UsbDeviceHandleWin::GotDescriptorFromNodeConnection(
    TransferCallback callback,
    scoped_refptr<base::RefCountedBytes> request_buffer,
    scoped_refptr<base::RefCountedBytes> original_buffer,
    Request* request_ptr,
    DWORD win32_result,
    size_t bytes_transferred) {
  std::unique_ptr<Request> request = UnlinkRequest(request_ptr);
  if (win32_result != ERROR_SUCCESS) {
    SetLastError(win32_result);
    USB_PLOG(ERROR) << "Failed to read descriptor from node connection";
    std::move(callback).Run(UsbTransferStatus::TRANSFER_ERROR, nullptr, 0);
    return;
  }

解决方案

这个错误是无害的,不会阻止新的浏览上下文的产生,即Chrome浏览器 session。所以你可以安全地忽略这个错误。

但是在您的代码块中,您需要将 关键字 resource_path 替换为 executable_path 并且您的有效代码块将是:

webdriver.Chrome(executable_path=r'C:\webdriver\chromedriver.exe') # to open the chromebrowser 
driver.get("https://web.whatsapp.com")

参考资料

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

在为我的错误找到答案一周后,我得到了一个解决方案,你只需要安装 pywin32 库,它不会给你一个错误

打开cmd并输入

pip 安装 pywin32

一切顺利......!

检查您的设备列表,看看是否有任何 USB 设备不工作。 我的笔记本电脑启用了蓝牙和摄像头设备后,我解决了这个问题。

这是一个 chromedriver 问题,他们仍在努力解决问题。我不是完全确定是什么原因造成的,但技术细节似乎在 Debanjan 的回答中有详细说明。

互联网上的一般解决方案似乎只是“忽略它”,但它确实使日志变得杂乱无章。

不过我确实找到了让它闭嘴的方法(以及经常弹出的“DevTools”警告)。

options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)

除了根据需要指向您的 chromedriver 可执行文件之外,您还可以添加其他 chromedriver 选项和开关。

我试过很多方法,但none对我有用。

我能做的就是用时间模块延迟Chrome浏览器的自动退出。

from selenium import webdriver
import chromedriver_binary  # Adds chromedriver binary to path
import time

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options)

driver.get('https://www.google.com/')
time.sleep(3600) # let the browser die after 1 hour

请检查一次规范文件名。我已经重命名了规范文件,但忘记在配置文件中更新它,所以得到这个错误。后来我改了。解决了

解决方案:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

options = Options()

options.add_experimental_option('excludeSwitches', ['enable-logging'])

driver = webdriver.Chrome(executable_path=r"D:\SW\chromedriver90\chromedriver.exe",chrome_options=options)

url = 'https://carlsagan.com/'

driver.get(url)

...

driver.quit()

我找到了一个修复程序!!首先下载 this chrome extension 然后点击扩展,并将用户代理设置为“Mac 上的 Chrome”

第二次更改代码中的这一行

driver = webdriver.Chrome(resource_path("C:\webdriver\chromedriver.exe"))  # to open the chromebrowser
driver.get("https://web.whatsapp.com")

至:

driver = webdriver.Chrome(executable_path=r'C:\webdriver\chromedriver.exe')  # to open the chromebrowser
driver.get("https://web.whatsapp.com")

同时打开 cmd 并输入:

pip install pywin32

错误

[15292:18896:0820/144926.111:ERROR:device_event_log_impl.cc(214)] [14:49:26.110] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

在为我安装的 chrome 版本下载并使用所需的网络驱动程序版本后消失了。参见 https://chromedriver.chromium.org/downloads

options.add_experimental_option("excludeSwitches", ["enable-logging"]) 无需安装 pywin32 就帮我解决了问题。