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

使用Selenium无界面模式下载PDF失败,请求技术协助

解决Selenium渲染下载PDF失败的问题

以下是针对你代码问题的几个修复方向和优化方案:

  • 确保Chrome与ChromeDriver版本匹配
    ChromeDriver版本必须和本地Chrome浏览器大版本完全一致(比如Chrome 118.x对应ChromeDriver 118.x),版本不兼容会直接导致下载逻辑失效。

  • 补充关键的Chrome偏好设置
    你的现有配置缺少触发自动下载的核心参数,修改后的profile配置如下:

    profile = {
        "plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
        "download.default_directory": download_dir,
        "download.prompt_for_download": False,
        "safebrowsing_for_trusted_sources_enabled": False,
        "download.open_pdf_in_system_reader": False,
        "plugins.always_open_pdf_externally": True,  # 强制PDF下载而非在线预览
        "download.directory_upgrade": True  # 允许系统修改下载目录
    }
    
  • 等待页面完全渲染完成
    渲染生成的PDF需要加载时间,直接调用driver.get(link)后可能还未触发下载,需添加显式等待逻辑:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver.get(link)
    # 等待PDF渲染元素加载(根据页面实际情况调整定位器)
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.TAG_NAME, "embed"))
    )
    
  • 处理页面交互需求
    如果生成PDF需要登录、点击生成按钮等前置操作,必须补充对应交互代码,比如:

    # 示例:完成登录操作
    driver.find_element(By.ID, "username").send_keys("your_account")
    driver.find_element(By.ID, "password").send_keys("your_pwd")
    driver.find_element(By.ID, "submit-btn").click()
    # 点击生成PDF按钮
    driver.find_element(By.CLASS_NAME, "generate-pdf").click()
    
  • 无头模式优化(可选)
    若需无界面运行,使用Chrome新无头模式并补充必要参数:

    options.add_argument("--headless=new")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=1920x1080")  # 确保页面完整渲染
    

修改后的完整代码示例:

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

download_dir = "C:\\Users\\UserName\\Desktop\\NewFolder\\"
options = webdriver.ChromeOptions()
profile = {
    "plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}],
    "download.default_directory": download_dir,
    "download.prompt_for_download": False,
    "safebrowsing_for_trusted_sources_enabled": False,
    "download.open_pdf_in_system_reader": False,
    "plugins.always_open_pdf_externally": True,
    "download.directory_upgrade": True
}
options.add_experimental_option("prefs", profile)
# 可选开启无头模式
# options.add_argument("--headless=new")
# options.add_argument("--disable-gpu")

service = Service()
driver = webdriver.Chrome(service=service, options=options)

# 若需前置交互,在此处补充代码
# driver.find_element(By.ID, "login-btn").click()

driver.get(link)
# 等待PDF渲染完成
WebDriverWait(driver, 15).until(
    EC.presence_of_element_located((By.TAG_NAME, "embed"))
)

# 可选:监控下载目录,确认文件生成后再关闭浏览器
# import os
# import time
# while not any(f.endswith('.pdf') for f in os.listdir(download_dir)):
#     time.sleep(1)

driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 21:36:28