1

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

----------------------------------------
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60

1 Answers1

1

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

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • If i do so in silent mode such as suggested by you: profile_options.set_preference("print.always_print_silent", True) I get the error `The selected printer could not be found` – Slartibartfast Sep 08 '21 at 05:21
  • 1
    I changed another line in the code that I posted to this question. Please run it as is and tell me what happens. – Life is complex Sep 08 '21 at 11:55
  • Thanks, the code is working great! I got a `testpage.pdf` generated with the pdf print. Its amazing that you were able do this for PC with a mac. Amazing!!! – Slartibartfast Sep 08 '21 at 17:50
  • I'm happy that it's working for you. I have written a lot of `selenium` code and I have found that most of it can be reused on other platforms with a few modifications. I write `selenium` code for all the browsers (e.g., chrome, mozilla and edge). – Life is complex Sep 08 '21 at 23:08