Selenium 打印错误找不到所选的打印机

Selenium printing error The selected printer could not be found

参考@lifeiscomplex提供的解决方案

密码是:

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()
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.5; rv:90.0) Gecko/20100101 Firefox/90.0'
profile_options.set_preference('profile_options = FirefoxProfile()', 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", True)
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)

# set your own file path
profile_options.set_preference('print.printer_Mozilla_Save_to_PDF.print_to_file.print_to_filename',
                               "testprint.pdf")
driver = webdriver.Firefox(executable_path=driver_path, options=firefox_options,
                           firefox_profile=profile_options)


URL = 'https://finance.yahoo.com/'


driver.get(URL)
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)

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

当我 运行 这段代码时,代码执行本身不会产生任何错误,但我在 firefox 中得到这个错误对话框。即使您按 Ctrl+P,您也会得到相同的对话框:

但是,如果在页面加载过程中手动单击 FireFox 中的 application menu 打印,我会得到打印对话框:

你能告诉我为什么会出现那个对话框吗?

我确实在 运行 脚本之前将开关更改为 True,这应该无关紧要,因为它已经在 profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.print_to_file", True) 中:

编辑:

通过注释掉这一行,我能够让打印对话框开始重新出现:

profile_options.set_preference("print.always_print_silent", True)

但是我找不到应该生成的文件testpage.pdf


My system information
----------------------------------------
Platform:    Windows
OS:          10
Python:      3.8.8
Selenium:    3.141.0
Firefox:     90.0.2
Geckodriver: 0.29.0

----------------------------------------

在这个问题的评论中 你提到你已经从我的回答中更改了这一行:

profile_options.set_preference('print.printer_Mozilla_Save_to_PDF.print_to_file.print_to_filename', "testprint.pdf")

对此:

profile_options.set_preference('print.printer_Microsoft_Print_to_PDF.print_to_filename', "testprint.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()
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.5; rv:90.0) Gecko/20100101 Firefox/90.0'
profile_options.set_preference('profile_options = FirefoxProfile()', user_agent)

# this line was changed
profile_options.set_preference("print_printer", "Microsoft Print to PDF")

profile_options.set_preference("print.always_print_silent", True)
profile_options.set_preference("print.show_print_progress", True)
profile_options.set_preference('print.save_as_pdf.links.enabled', True)

# this line was changed
profile_options.set_preference("print.printer_Microsoft_Print_to_PDF.print_to_file", True)

# this line was changed
profile_options.set_preference('print.printer_Microsoft_Print_to_PDF.print_to_filename', "testprint.pdf")

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


URL = 'https://finance.yahoo.com/'


driver.get(URL)
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)

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

可能还有另一行需要更改,但我无法测试上面的代码来验证这一点,因为我使用的是 macOS 而不是 Windows 10.

您还提到您注释掉了这一行:

profile_options.set_preference("print.always_print_silent", True)

注释掉此行将启动 print dialog,如下图所示。