1

Does anyone know of an API that allows shortcuts to be executed on Google Chrome web browser (such as CRTL+P - which is print; or CRTL+T - which is open new tab).

This question comes as it doesn't appear that Selenium has such a capacity as its send_keys functionality require an element on the web page rather than being actioned from a browser object.

1 Answer 1

1

While I can't directly answer the question, I'd like to point out that Selenium can use an XPath to retrieve the page body as an element. It can then open the Print dialog using send_keys:

element = browser.find_element_by_xpath("//body")
element.send_keys(Keys.CONTROL, 'p')

However, this only seems to work in Firefox, and doesn't do anything when using ChromeDriver. I tried playing around with implicit and explicit waits, but no dice.

Sign up to request clarification or add additional context in comments.

4 Comments

Much appreciated for the effort.
Any idea how to automatically press return on the print window to trigger it off? Is the a way to simulate return keyboard press outside webbrowser scope?
Hmm. I could swear I'd seen a method to do this, but searching now I can't find it, and I've bumped into a few sources suggesting that it's not possible. As for your second question, I'm sure something could be set up using AutoHotKey or similar tools, but it'd be a rather messy solution!
@Robert, you can simply press escape to close print window: browser.find_element_by_xpath("//body").send_keys(Keys.ESCAPE). But first you should switch to the print window, so use this beforehand: browser.switch_to_window(browser.window_handles[-1]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.