Selenium printing error The selected printer could not be found

This is in reference to the solution provided by @lifeiscomplex How to handle Firefox print dialog box in Selenium

The code is:

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()

When i run this code, the code executes itself does not produce any error but i get this error dialogbox in firefox. Even if you press Ctrl+P you will get the same dialog box: enter image description here

However if manually clicked on print from application menu in FireFox during the page loading process i do get the print dialog: enter image description here

Could you please advise why am i getting that dialogbox.

I did change the toggle to True before running the script, which shouldn’t matter since since its already in profile_options.set_preference("print.printer_Mozilla_Save_to_PDF.print_to_file", True): enter image description here

Edit:

I was able to get the print dialog box to start reappearing by commenting out this line:

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

But i am not able to find the file testpage.pdf which is suppose to be generated.


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

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

Answer

In the comments of this question “How to handle Firefox print dialog box in Selenium” you mentioned that you had changed this line from my answer:

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

to this:

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

If you want to change that line you would have to change at least one more in the code you posted.

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()

There might be another line that needs to be changed, but I cannot test the code above to verify this, because I’m using macOS and not Windows 10.

You also mentioned that you commented this line out:

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

commenting out this line will launch the print dialog as showed in the graphic below.

enter image description here