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

Selenium 4.20.0:Chrome更新后未渲染爬取数据问题求助

问题:Chrome更新后Selenium启动浏览器仅显示"data;"无页面渲染

我使用Python进行网页数据爬取,当前使用Selenium 4.20.0版本,此前未安装ChromeDriver的情况下脚本运行正常。但在Chrome更新至126.0.6478.91版本后,运行脚本时Chrome虽能启动,但无法渲染任何数据,标签页及地址栏仅显示"data;"。

相关代码

import configparser
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException
from openpyxl import load_workbook
import time
import os
import logging

# 获取当前用户的下载路径
config = configparser.ConfigParser()
# 配置日志
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def run_script(config):

    config.read('credentials.ini')
    # 加载工作簿
    book = config ['Credentials']['saveFilePath']
    wb = load_workbook(book)
    ws = wb.active
    # 清空工作表内容
    ws.delete_rows(2, ws.max_row)
    # 找到工作表的下一个空行
    next_row = ws.max_row + 1

    username = config['Credentials']['username']
    password = config['Credentials']['password']
    AgencyFilePath = config['Credentials']['AgencyFilePath']
    workbook = load_workbook(AgencyFilePath)
    sheet = workbook.active

    try: 
        driver = webdriver.Chrome()

        login_url = 'https://www.google.com'
        driver.get(login_url)

        wait = WebDriverWait(driver, 30)

        username_field = wait.until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="lock-title"]'))
        )

        # 通过类名找到用户名、密码输入框和提交按钮
        username_field = driver.find_element(
            By.XPATH, '//*[@id="1-email"]' # 用户名输入框的xpath
        ).send_keys(username)

        password_field = driver.find_element(
            By.XPATH, '//*[@id="1-password"]' # 密码输入框的xpath
        ).send_keys(password)

问题截图

Chrome地址栏显示"data;"的异常截图


解决方案

1. 修复版本兼容性问题

Selenium 4.20.0对Chrome 126的小版本兼容性存在瑕疵,可二选一解决:

  • 升级Selenium到最新稳定版(如4.21.0及以上),新版本会同步适配Chrome的最新小版本;
  • 降级Chrome到124或125稳定版,与Selenium 4.20.0的官方兼容范围完全匹配。

2. 手动指定ChromeDriver(或用自动管理工具)

Selenium自带的自动Driver匹配偶尔会失效,手动指定更可靠:

  1. 下载与当前Chrome版本完全一致的ChromeDriver;
  2. 在代码中显式指定Driver路径:
from selenium.webdriver.chrome.service import Service
# 替换为你的ChromeDriver实际路径
driver = webdriver.Chrome(service=Service("C:/chromedriver/chromedriver.exe"))

或者使用webdriver-manager自动管理依赖,彻底避免版本不匹配:

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

3. 添加Chrome启动参数排除冲突

尝试禁用可能导致页面加载异常的特性,修改Driver初始化代码:

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)

4. 清理Chrome缓存与临时数据

Chrome更新后残留的旧数据可能干扰加载,启动时指定全新的临时用户目录:

options = webdriver.ChromeOptions()
# 自定义一个临时目录路径,确保目录存在
options.add_argument("--user-data-dir=C:/temp/chrome_temp_profile")
driver = webdriver.Chrome(options=options)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 23:57:36