7

Once a browser page is loaded I'm looking to use the CRTL+P shortcut in Goggle Chrome to enter the print page and then simply press return to print the page.

import time
from selenium import webdriver

# Initialise the webdriver
chromeOps=webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)

# Login to Webpage
browser.get('www.webpage.com')

My question is how do I send keys to the browser itself rather than an element?

Failed Attempt: To assign the html body to as the element and send keys to that-

elem = browser.find_element_by_xpath("/html/body") # href link
elem.send_keys(Keys.CONTROL + "P")      # Will open a second tab
time.sleep(3)
elem.send_keys(Keys.RETURN)
Phoenix
  • 4,386
  • 10
  • 40
  • 55
  • The 'usual' method is to do as you are, target the `` element and `.send_keys()` to that. Obviously, this isn't working for you but what is or isn't happening? If the second tab opens, have you tried changing to that tab and then `.send_keys(Keys.RETURN)`? – Mark Rowlands Feb 20 '14 at 12:11
  • .send_keys() to the body isn't working. I'm not getting the print preview pane to come up. – Phoenix Feb 20 '14 at 12:21
  • Ah your code comment confused me, I thought you meant that it was opening the second tab. – Mark Rowlands Feb 20 '14 at 12:47
  • If one was to set off CRTL+t for open a new tab then CRTL+p (which I want) would be working at the same level. If its possible to get crtl+t to work then the same code would support crtl+p. – Phoenix Feb 20 '14 at 13:04
  • I am having the same issue and none of the answers here didn't work for me. I also noticed that even if CTRL+P, CTRL +T doesn't work which opens some popup or browser tab, some key combinations like CTRL+A is working. – Sahan Feb 10 '22 at 13:39

4 Answers4

3

I have tested this on Google Chrome and the problem can be solved using a combination of .key_down() and .send_keys() methods of the ActionChains class.

ActionChains(driver).key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL).perform()
ActionChains(driver).send_keys(Keys.ENTER)
GPT14
  • 809
  • 6
  • 13
  • @RaunakThomas: You can use the above code to simulate almost every keyboard shortcut there is. I have tested this with `ctrl + f` `ctrl + p`, cut, copy, paste. Important thing to remember, 1. `key_down()` method should only be used with modifier keys (Control, Alt and Shift). 2. there should always be a `.key_up()` call made after the `.key_down()` method. – GPT14 Jun 05 '18 at 07:17
  • I just realized that for some weird reason when you put this code just below the code that starts the webdriver it works. But once the webdriver is open already it does not work if I try to run just this one line of code. I'm guessing its something to do with the active window but I cant seem to figure that out – Raunak Thomas Jun 05 '18 at 07:18
  • @RaunakThomas Can you please elaborate? – GPT14 Jun 05 '18 at 07:29
  • I'm using Jupyter notebook. In one cell if I write `driver = webdriver.Chrome(chromedriver)` and then open a website and use your shortcut it works. But if I run only the code above in one cell and then `alt` + `tab` manually to come back to my code and then run your code in a separate cell it runs with no error but your code does not work – Raunak Thomas Jun 05 '18 at 07:39
  • Can you please raise a new question and tag me in the comment? Please include all this information in it. – GPT14 Jun 05 '18 at 07:44
  • Unable to tag you in the new question, but [here](https://stackoverflow.com/questions/50713695/actionchains-perform-not-working-when-driver-loses-focus) is the link – Raunak Thomas Jun 06 '18 at 06:40
1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser.get('https://www.myglenigan.com/project_search_results.aspx?searchId='+ID)
element=browser.find_element_by_xpath("//body")
element.send_keys(Keys.CONTROL, 'p')

Just a note, this will open Firefox print panel. But the same code will not work in Goggle Chrome.

Phoenix
  • 4,386
  • 10
  • 40
  • 55
0
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

ActionChains(browser).send_keys(Keys.CONTROL, "p").perform()

that would send the keyboard shortcut for a print dialog

I haven't found a way to do this in FF for printing though - ctrl+p will open the print dialog, but FF has a focus bug that doesn't allow one to do Keys.ENTER for the dialog itself

hopefully this will work for you in Chrome, I haven't tested it there

please update if you find a way around this - possibly try AutoIt

If none of the above works you can always do

browser.get_screenshot_as_file( path + 'page_image.jpg' )
sam-6174
  • 3,104
  • 1
  • 33
  • 34
0

If i understood your question correctly my suggestion is that you install and use the pyautogui module to make your python program press keys

For example:

import pyautogui
pyautogui.hotkey('ctrl','p')

see the pyautogui documentation for more information: https://pyautogui.readthedocs.io/en/latest/introduction.html

Nick Ven
  • 91
  • 1
  • 10
  • How do you ensure Ctrl-p is going to go to the browser instance started by Selenium and *not* some other window? – Louis Jun 04 '18 at 11:48
  • I don't think you can. It's just an alternative I wanted to mention because no answer has been accepted yet. Although I think there are image recognition functions in this module that maybe could help – Nick Ven Jun 04 '18 at 11:51
  • This can not be used in headless task. User have to keep the window open, otherwise it will keep pressing on other window which is opened. – Atish Paul Apr 13 '22 at 05:49