使用ChromeDriver Selenium在Windows操作系统上出现了“从节点连接读取描述符失败:系统连接的设备不起作用”错误。

45

我在使用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")

3
@DebabjanB,你最终解决了这个问题吗?我也遇到了同样的问题。 - ranni rabadi
在Chrome浏览器中,我按照chrome://flags的步骤启用了“启用新的USB后端”选项,此后日志消息消失了。请参考https://dev59.com/UlEG5IYBdhLWcg3wf_nV#65134639。 - klapshin
1
@klapshin 我也遇到了这个“无法读取...”的消息 - 在我的C#-Selenium-VS Code 2019项目中。我去了chrome://flags,但没有“启用新的USB后端选项”,或者任何与USB有关的东西。 - Anne Bailly
@AnneBailly 最近他们已经修复了 https://bugs.chromium.org/p/chromium/issues/detail?id=637404 因此,可能需要更新我们的chromedriver版本到最新版本,或者回滚到先前的Chrome+chromedriver版本,其中有这样一个选项。 为了检查这是否真的是USB问题,请拔掉USB设备并查看错误是否消失。 - klapshin
12个回答

59

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

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

不过我找到了一种方法来让它保持安静(以及经常弹出的“DevTools”警告)。

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

如果您希望的话,除了指向 chromedriver 可执行文件之外,还可以将其他 chromedriver 选项和开关添加到其中。


5
对于任何想知道“Options”应该是什么的人,我发现使用options = webdriver.ChromeOptions()可以解决问题。 - Niko Pasanen
你说得对。它不让我审核编辑。这取决于你如何设置Chrome驱动程序,但在大多数情况下都可以工作。干得好! - Rue Lazzaro
@RueLazzaro 在你的代码或框架中,你把这个选项代码块放在哪里了?我在我的C#-Selenium-VS Code 2019项目中收到了“无法读取...”的消息。我把那个代码块放在类的不同位置,但是会出现编译错误。我是一个初学者,所以我可能做错了什么。 - Anne Bailly
@AnneBailly 不用担心!这都是Python。我不确定如何在C#中设置它。很抱歉!但我相信有人可以帮助你。在Python中,您需要在实际启动驱动程序并执行操作(例如启动网页并单击某些内容)之前定义所有内容。 - Rue Lazzaro
这将使用C#完成: var options = new ChromeOptions(); options.AddExcludedArguments(new List {"excludeSwitches", "enable-logging"}); var driver = new ChromeDriver(options); - Dan Marshall

16

这个错误信息...

[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)

这意味着在尝试启动/生成新的浏览上下文即Chrome浏览器会话时,ChromeDriver发生了错误。


分析

此错误是由于连接到系统且未正常工作的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浏览器会话)的生成造成影响,因此可以安全地忽略这个错误。

然而,在您的代码块中,您需要用executable_path替换关键词resource_path,这样您的有效代码块将是:

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

参考资料

你可以在以下链接中找到几个相关的详细讨论:


2
我没有连接任何USB设备。我需要摆脱这个错误,因为我需要将这个脚本转换成一个窗口".exe"文件以在任何窗口机器上运行。 - UMANG BARAIYA
@UMANGBARAIYA 这个错误来自于 Chrome/ChromeDriver 的回调函数,不会影响您的测试和测试结果。生成的 exe 文件也将完美运行。 - undetected Selenium
这对我没有起作用。驱动程序确实启动了,但我是通过Python提示符输入命令的,当我要输入下一个命令时,提示符会出现错误。我担心如果我将编写任何脚本,这个错误将不会让脚本工作。 - Vinamra Bali

10

在寻找一个解决我的错误的答案一周后,我最终找到了一个解决方案,只需要安装pywin32库即可,这样就不会出现错误。

打开命令提示符并输入

pip install pywin32

然后你就可以开始了.....!


你好,只需要安装pywin32吗?不需要导入吗?因为我已经安装了,但是错误仍然存在... - LandiLeite
2
也没有帮助到我。 - Satria
不错,这很有帮助!可能是因为驱动程序是32位的?(因为在Win上只有这一个可用) - Ron Al
事后因果推断 - Alex Deft

7
解决方案:
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()

嗨@ Dennis,你能告诉我如何将无头选项与这个实验选项一起包括吗?先谢谢! - Digital Farmer

2
我找到了一个解决方法!首先下载这个Chrome扩展程序,然后点击扩展程序,将用户代理设置为“Mac上的Chrome”。
其次,在您的代码中更改此行。
driver = webdriver.Chrome(resource_path("C:\\webdriver\\chromedriver.exe"))  # to open the chromebrowser
driver.get("https://web.whatsapp.com")

to:

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

同时打开命令提示符并输入:

pip install pywin32

1

检查您的设备列表,看看是否有任何USB设备无法工作。在我的笔记本电脑启用蓝牙和摄像头设备后,我解决了这个问题。在此输入图片描述


1
我在运行此代码时没有连接任何类型的USB设备,而且该代码不需要任何类型的USB设备来运行,它是完全独立的。 - UMANG BARAIYA

0
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)

这对我有用,希望能帮到别人。


0
错误
[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版本所需的Web驱动程序版本后,它消失了。请参见https://chromedriver.chromium.org/downloads


注意:我正在使用Robot Framework和Selenium库。 - DaveX

0
错误可能是由于您在 resource_path 变量中使用了括号。 代码应该如下所示:
driver = webdriver.Chrome(resource_path="C:\webdriver\chromedriver.exe") # to open the chromebrowser 
driver.get("https://web.whatsapp.com")

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


我尝试了你的方法,但仍然出现了那个错误...! - UMANG BARAIYA

0

options.add_experimental_option("excludeSwitches", ["enable-logging"]) 对我来说起到了作用,而无需安装pywin32。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接