You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium爬取网站时遇USB/Bluetooth错误的解决需求

Selenium Chrome驱动USB/Bluetooth警告消除方案

问题说明

运行Selenium爬虫代码时,控制台输出以下警告日志:

USB: usb_device_handle_win.cc:1045 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.

这类警告是Chrome驱动尝试访问系统USB、蓝牙设备失败导致的,属于非致命日志,不会影响爬虫的正常执行,但如果需要消除这些输出,可按以下方案处理。

解决方案

1. 添加日志抑制选项

在Chrome配置中添加参数,禁用不必要的日志输出:

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# 抑制驱动日志输出
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
# 额外添加自动化特征屏蔽,避免网站检测(可选)
chrome_options.add_argument('--disable-blink-features=AutomationControlled')

2. 代码细节优化(可选)

原代码存在几个可优化点,提升稳定性和准确性:

  • 路径字符串使用原始字符串,避免转义字符问题:将'I:\chromedriver_win32\chromedriver.exe'改为r'I:\chromedriver_win32\chromedriver.exe'
  • 定位表格行时使用更精准的XPath:用//table//tr代替tr,避免获取页面中无关的tr元素
  • 使用显式等待替代直接查找元素,提升页面加载不稳定时的鲁棒性

优化后的完整代码:

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

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# 抑制日志输出
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.add_argument('--disable-blink-features=AutomationControlled')

# 原始字符串处理路径
service = Service(r'I:\chromedriver_win32\chromedriver.exe')
target_url = 'https://www.adamchoi.co.uk/overs/detailed'

driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get(target_url)

# 显式等待按钮可点击后操作
all_matches_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//label[normalize-space()='All matches']"))
)
all_matches_btn.click()

# 精准获取表格内的比赛行
match_rows = driver.find_elements(By.XPATH, "//table//tr")
for row in match_rows:
    print(row.text)

内容的提问来源于stack exchange,提问作者Muneeb Ur rehman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 04:32:32