Python - Selenium - How to use Browser Shortcuts

15,484

Solution 1

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)

Solution 2

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.

Share:
15,484
Phoenix
Author by

Phoenix

Updated on June 29, 2022

Comments

  • Phoenix
    Phoenix almost 2 years

    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
    Phoenix about 10 years
    Thanks for reply, but this code doesn't appear to work for Goggle Chrome for me.
  • Raunak Thomas
    Raunak Thomas almost 6 years
    Yes it is not working in Google Chrome, specifically with chromedriver_v2.27
  • Louis
    Louis almost 6 years
    How do you ensure Ctrl-p is going to go to the browser instance started by Selenium and not some other window?
  • Nick Ven
    Nick Ven almost 6 years
    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
  • GPT14
    GPT14 almost 6 years
    @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.
  • Raunak Thomas
    Raunak Thomas almost 6 years
    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
  • GPT14
    GPT14 almost 6 years
    @RaunakThomas Can you please elaborate?
  • Raunak Thomas
    Raunak Thomas almost 6 years
    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
  • GPT14
    GPT14 almost 6 years
    Can you please raise a new question and tag me in the comment? Please include all this information in it.
  • Raunak Thomas
    Raunak Thomas almost 6 years
    Unable to tag you in the new question, but here is the link
  • Atish Paul
    Atish Paul about 2 years
    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.