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

Selenium出现WebDriverException: DevTools连接断开问题求助

解决Selenium爬取Google Maps时的DevTools断开及滚动异常问题

环境信息

  • OS:13.2.1(M2芯片)
  • 开发环境:Miniconda+Spyder
  • Python版本:3.10.9
  • 问题表现:原本滚动功能正常但无法触底,优化后滚动时长逐渐缩短,最终触发WebDriverException: disconnected: not connected to DevTools错误,已尝试重装驱动、更换Chrome/Chromium/Brave浏览器,无手动操作窗口行为。

解决方案

1. 修复DevTools连接异常(核心)

M系列Mac的Chrome系浏览器存在DevTools兼容性问题,添加启动参数强制稳定连接:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 固定调试端口,避免连接异常
chrome_options.add_argument("--remote-debugging-port=9222")
# 禁用共享内存,解决M芯片进程崩溃问题
chrome_options.add_argument("--disable-dev-shm-usage")
# 禁用GPU加速,避免渲染冲突
chrome_options.add_argument("--disable-gpu")
# 指定Brave浏览器路径(如果用Chrome/Chromium则对应修改路径)
chrome_options.binary_location = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"

driver = webdriver.Chrome(
    service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()),
    options=chrome_options
)

2. 优化滚动逻辑(解决触底失败+时长缩短)

替换固定步长滚动和硬编码高度判断,改用动态滚动+显式等待:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# 等待评论容器加载完成
comment_container = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]'))
)

last_height = driver.execute_script("return arguments[0].scrollHeight", comment_container)

while True:
    # 直接滚动到容器底部
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight;', comment_container)
    try:
        # 等待新内容加载,最多等待8秒,直到高度变化
        WebDriverWait(driver, 8).until(
            lambda d: driver.execute_script("return arguments[0].scrollHeight", comment_container) > last_height
        )
        last_height = driver.execute_script("return arguments[0].scrollHeight", comment_container)
    except:
        # 高度不再变化,说明已到页面底部
        break

3. 稳定元素定位(避免遍历低效问题)

替换遍历所有div找评论按钮的逻辑,改用精准定位:

# 点击评论按钮,优先用CSS选择器定位
try:
    review_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label*="reviews"]'))
    )
    review_button.click()
except:
    # 备选XPATH定位,适配页面结构变化
    review_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//div[contains(text(), "reviews") and @role="button"]'))
    )
    review_button.click()

4. 额外排查点

  • 版本匹配:确保ChromeDriver与浏览器版本严格一致,可手动指定版本号安装:
    ChromeDriverManager(chrome_type=ChromeType.BRAVE, version="110.0.5481.177").install()
    
  • Spyder模式:运行脚本时切换到「Run without debugging」模式,关闭实时变量监控,避免干扰WebDriver进程。

内容的提问来源于stack exchange,提问作者TSD

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 19:03:00