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

Selenium调用Firefox静默打印报错找不到选定打印机 生成PDF缺失

问题原因分析
  • 代码存在配置错误:设置UA的代码行profile_options.set_preference('profile_options = FirefoxProfile()', user_agent)参数键名完全错误,该错误会导致整个Firefox配置文件加载异常,后续打印相关配置未生效,浏览器找不到指定的PDF虚拟打印机,触发报错。
  • 输出路径为相对路径:代码中设置的testprint.pdf是相对路径,Selenium启动的Firefox实例默认工作目录不是脚本所在目录,生成的文件会被保存到Firefox默认工作目录、Geckodriver所在目录或系统临时目录,导致无法找到生成的PDF文件。
  • 配置参数不全:Firefox 89及以上版本调整了静默打印的相关配置,缺少页面大小、边距等基础打印配置时,会触发打印校验失败,提示找不到打印机。
  • 版本适配问题:Geckodriver 0.29.0对Firefox 90的静默打印配置适配存在已知缺陷,部分参数无法正确传递。
解决方法

第一步:修正代码配置

替换原有代码为如下版本,注意将PDF输出路径修改为你本地的绝对路径:

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.options import FirefoxProfile

driver_path = 'geckodriver.exe'
firefox_options = Options()
firefox_options.add_argument("--disable-infobars")
firefox_options.add_argument("--disable-extensions")
firefox_options.add_argument("--disable-popup-blocking")

profile_options = FirefoxProfile()
# 修正UA设置的键名,不需要自定义UA可直接删除以下两行
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.5; rv:90.0) Gecko/20100101 Firefox/90.0'
profile_options.set_preference('general.useragent.override', user_agent)

# 打印基础配置
profile_options.set_preference("print_printer", "Mozilla Save to PDF")
profile_options.set_preference("print.always_print_silent", True)
profile_options.set_preference("print.show_print_progress", False)
profile_options.set_preference('print.save_as_pdf.links.enabled', True)

# 虚拟打印机配置
profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.print_to_file", True)
# 替换为你本地的绝对路径,比如r"C:\Users\用户名\Desktop\testprint.pdf"
profile_options.set_preference('print.printer_Mozilla_Save_to_PDF.print_to_file.print_to_filename',
                               r"替换为你的绝对路径\testprint.pdf")

# 补全打印基础参数,避免校验失败
profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.paper_size", 0) # A4纸
profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.margin_top", 0.5)
profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.margin_bottom", 0.5)
profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.margin_left", 0.5)
profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.margin_right", 0.5)

driver = webdriver.Firefox(executable_path=driver_path, options=firefox_options,
                           firefox_profile=profile_options)

URL = 'https://finance.yahoo.com/'
driver.get(URL)
sleep(3) # 增加页面加载等待时间,避免页面未加载完成触发打印异常

search_field_id = 'yfin-usr-qry'
element_search_field = driver.find_element_by_id(search_field_id)
element_search_field.clear()
element_search_field.send_keys('TSLA')
element_search_field.send_keys(Keys.ENTER)
sleep(5) # 等待搜索结果加载完成

driver.execute_script("window.print()")
sleep(10)
driver.quit()

第二步:版本适配调整

如果修正代码后仍然存在报错,将Geckodriver升级到0.30.0版本,该版本完美适配Firefox 90的打印相关配置。

补充说明

如果不需要静默打印,注释掉profile_options.set_preference("print.always_print_silent", True)行后,只要设置了绝对路径,手动点击打印确认后PDF文件也会直接保存到指定路径,不会再出现找不到文件的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:39:00