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

Python3.6+Selenium Firefox驱动二次请求立即返回,文件下载异常

解决Selenium Firefox驱动第二次下载请求失效的问题

我之前也碰到过一模一样的情况——第一次下载顺顺利利,第二次点击下载直接没反应,驱动就跟“躺平”了似的。结合我的踩坑经验,大概率是Firefox配置没到位或者会话状态处理不当导致的,咱们一步步来修复:

1. 先把FirefoxProfile的下载配置拉满

默认的FirefoxProfile可能会弹出下载确认框,或者没有正确绑定下载目录,第二次请求时这些配置可能没生效。你得把自动下载的参数都配置好:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

def get_configured_driver():
    # 初始化Firefox配置文件
    profile = webdriver.FirefoxProfile()
    
    # 核心下载配置
    profile.set_preference("browser.download.folderList", 2)  # 2代表自定义下载路径
    profile.set_preference("browser.download.dir", "/Users/yourname/Downloads/test")  # 替换成你的下载路径
    # 根据你要下载的文件类型添加MIME类型,比如PDF、zip、exe等
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/pdf,application/zip")
    profile.set_preference("pdfjs.disabled", True)  # 禁用内置PDF阅读器,直接下载
    profile.set_preference("browser.download.manager.showWhenStarting", False)  # 隐藏下载管理器弹窗
    profile.set_preference("browser.download.manager.useWindow", False)  # 不弹出下载窗口
    
    # 把配置传入Options,初始化驱动
    options = Options()
    options.profile = profile
    driver = webdriver.Firefox(options=options)
    return driver

2. 用显式等待替代直接找元素

你之前的代码直接用find_element_by_xpath,很可能第二次页面还没加载完就执行了点击,导致驱动找不到元素直接返回。换成WebDriverWait显式等待,确保元素真的可点击了再操作:

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

# 检查文件是否下载完成的辅助函数(比sleep更可靠)
def wait_for_download(download_dir, timeout=30):
    end_time = time.time() + timeout
    while time.time() < end_time:
        # 过滤掉临时文件(比如Firefox的.part文件)
        downloaded_files = [f for f in os.listdir(download_dir) if not f.endswith('.part')]
        if downloaded_files:
            return True
        time.sleep(1)
    return False

# 初始化配置好的驱动
driver = get_configured_driver()
download_path = "/Users/yourname/Downloads/test"

# 第一次下载
driver.get("第一个下载页面URL")
# 等待下载按钮可点击,最多等10秒
first_download_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//p[@class='download']/span"))
)
driver.execute_script("arguments[0].click();", first_download_btn)
# 等待第一次下载完成
wait_for_download(download_path)

# 第二次下载
driver.get("第二个下载页面URL")
second_download_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//p[@class='download']/span"))
)
driver.execute_script("arguments[0].click();", second_download_btn)
wait_for_download(download_path)

# 用完记得关闭驱动
driver.quit()

3. 检查版本兼容性

别忽略最基础的问题:geckodriver版本和Firefox浏览器版本必须匹配!比如Firefox 110+对应的geckodriver得是0.32+,版本不兼容会出现各种诡异的会话问题,你可以对照两者的版本发布说明确认适配关系。

4. 其他可能的坑

  • 如果网站需要登录,第二次请求时可能会话过期了,得重新执行登录流程;
  • 不要频繁创建销毁驱动实例,尽量复用同一个实例完成多次下载;
  • 如果下载的是动态生成的文件,可能需要等待页面完全渲染(可以加个driver.implicitly_wait(10)设置全局隐式等待)。

按照上面的步骤调整后,第二次下载应该就能正常触发了,我当时就是靠完善配置和显式等待解决的问题~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:36:14