1


I'm having a hard time initiating the 'print' event on google chrome using selenium. I've tried all the following both on OSX and Windows with no luck. For OSX I've replaced Keys.CONTROL with Keys.COMMAND/Keys.META.

driver = webdriver.Chrome()
driver.get("http://google.com")

# 1st try
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body'))
actions.key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL)
actions.perform()

# 2nd try
ActionChains(driver).key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL).perform()

# 3rd try
ActionChains(driver).send_keys(Keys.CONTROL, "p").perform()

# 4th try
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 'p')

None of the above did the trick. The only method that worked is driver.execute_script("window.print()"), but that is not the behaviour I'm looking for in this case.

Chrome driver version 80.0.3987.106.

Any ideas? Is there a way to initiate the 'print' event without the use of hotkeys?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    What do you mean window.print() is not hte behavior you're looking for? What is? – DMart Mar 10 '20 at 18:58
  • @DMart once window.print() is executed, the program will halt until the print window is closed. I need to access the DOM while the print dialog is open, but I can't do so. – Ishay Hilzenrat Mar 13 '20 at 12:03

1 Answers1

0

A few words:

  • You should not be testing print dialog as it is a part of browser functionality and not related to your application.
  • If your usecase is to test the browser but not the web application you should be aware that sticking to key events is not the best option to proceed as if/when you come to the situation of tests execution in Selenium Grid or any form of Parallel Test Execution you may run intorace condition.

Solution

So the way to go is Window.print() function instead and you can use either of the following strategies:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    In the question... `The only method that worked is driver.execute_script("window.print()"), but that is not the behaviour I'm looking for in this case.` – JeffC Mar 11 '20 at 19:09
  • As @JeffC mentioned, that is not the behaviour I'm looking for since once executed, the program will halt until the print window is closed. I need to access the DOM while the print dialog is open. – Ishay Hilzenrat Mar 13 '20 at 12:02