Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send keys not to element but in general selenium

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?

like image 244
micgeronimo Avatar asked Feb 12 '15 16:02

micgeronimo


People also ask

How do you send a key without an element?

We can send keys without specifying elements in Python with Selenium webdriver. The tagname input is used for all the edit boxes. We shall use the find_element_by_tag_name method and pass input as a parameter to that method. Thus we need not mention element attributes explicitly.

How do I press Enter key in Selenium without element?

We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.

What are the other alternate ways for sending keys in Selenium?

Now, as we discussed, Selenium WebDriver provides two ways to send any keyboard event to a web element: sendKeys() method of WebElement class. Actions class.


1 Answers

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

like image 53
Jamie Rees Avatar answered Sep 22 '22 03:09

Jamie Rees