22

Issue:

I want to send_keys(Keys.LEFT_CONTROL + 't') Now to do this I locate any element on the page

elem = self.browser.find_element_by_name('body')
elem.send_keys(Keys.LEFT_CONTROL + 't')

Problem is that each time I want to send above keys I have to locate some element, which actually I'm not interested in.

How can I send keys generally and not to specific object of page, I want something like self.browser.send_keys(Keys.LEFT_CONTROL + 't')? Is it even possible?

micgeronimo
  • 2,069
  • 5
  • 23
  • 44
  • 1
    Probably anyname is not unique for the target element – Saifur Feb 12 '15 at 16:12
  • If the target is a text box anyway, you might want to send to it directly: [java - In Selenium how do I find the "Current" object - Stack Overflow](https://stackoverflow.com/questions/7491806/in-selenium-how-do-i-find-the-current-object) – user202729 Jan 06 '21 at 16:11

1 Answers1

36

You are using WebDriver to interact with the actual elements on the page. It will not work.

Try using the Actions

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_CONTROL + 't')
actions.perform()

See the documentation: http://selenium-python.readthedocs.io/api.html?highlight=send_keys#module-selenium.webdriver.common.action_chains

Zuabi
  • 1,112
  • 1
  • 13
  • 26
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83