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

Ubuntu环境下Selenium控制Firefox下载完成弹窗关闭问题

解决Firefox下载完成弹窗无法关闭的问题

你在Ubuntu系统中使用Selenium操控Firefox时,遇到下载完成后右上角提示弹窗无法关闭、导致脚本无法正常结束的问题,可通过以下方式解决:

1. 补充关键的Firefox偏好设置

你已配置了部分下载相关偏好,但缺少控制下载完成通知的核心选项,需添加以下设置:

options.set_preference("browser.download.manager.notifyOnDownloadComplete", False)

该选项会直接禁用Firefox下载完成后的右上角通知弹窗,是解决此问题的关键。

2. 修正MIME类型字符串错误

你代码中browser.helperApps.neverAsk.saveToDisk的MIME类型存在语法错误(application/x-tar后多空格、application/单独成行无效),修正后的正确格式为:

options.set_preference(
    "browser.helperApps.neverAsk.saveToDisk",
    (
        "application/pdf, application/zip, application/octet-stream, "
        "text/csv, text/xml, application/xml, text/plain, "
        "text/octet-stream, application/x-gzip, application/x-tar, "
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    ),
)

错误的MIME类型可能导致Firefox仍弹出下载确认框,间接触发通知弹窗。

3. 优化脚本稳定性(可选)

替换代码中大量的time.sleep()为Selenium显式等待,能让脚本更健壮,避免因页面加载慢导致的异常:

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

# 初始化等待对象
wait = WebDriverWait(browser, 10)

# 等待用户名输入框出现
id_box = wait.until(EC.presence_of_element_located((By.ID, "id_username")))

修改后的完整脚本

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

options = webdriver.FirefoxOptions()

# 完整下载偏好设置
options.set_preference("browser.download.manager.showAlertOnComplete", False)
options.set_preference("browser.download.panel.shown", False)
options.set_preference("browser.download.animateNotifications", False)
options.set_preference("browser.helperApps.alwaysAsk.force", False)
options.set_preference("browser.download.manager.showWhenStarting", False)
# 禁用下载完成通知弹窗
options.set_preference("browser.download.manager.notifyOnDownloadComplete", False)

# 修正后的MIME类型列表
options.set_preference(
    "browser.helperApps.neverAsk.saveToDisk",
    (
        "application/pdf, application/zip, application/octet-stream, "
        "text/csv, text/xml, application/xml, text/plain, "
        "text/octet-stream, application/x-gzip, application/x-tar, "
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    ),
)

browser = webdriver.Firefox(options=options)
wait = WebDriverWait(browser, 10)

# 登录流程
browser.get('http://10.1.0.100:4025/accounts/login/?next=/')
id_box = wait.until(EC.presence_of_element_located((By.ID, "id_username")))
id_box.send_keys('admin')

pass_box = wait.until(EC.presence_of_element_located((By.ID, "id_password")))
pass_box.send_keys('changeme')

login_button = wait.until(EC.element_to_be_clickable((By.NAME, "submit")))
login_button.click()

# 备份生成流程
browser.get("http://10.1.0.100:4025/backup/list")
generate_button = wait.until(EC.element_to_be_clickable((By.ID, "generate")))
generate_button.click()

# 处理弹窗
try:
    alert = wait.until(EC.alert_is_present())
    alert.accept()
except:
    pass

# 等待备份生成(若有页面状态标识,建议替换为显式等待)
time.sleep(25)

# 触发下载
browser.get("http://10.1.0.100:4025/backup/download/all")
time.sleep(5)

# 彻底关闭浏览器进程
browser.quit()

注意:使用browser.quit()代替browser.close(),能确保完全终止Firefox进程,避免残留窗口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 17:01:34