46

I have a page whose source code is not available, but there is a input box where cursor is blinking.

Can i write something into the text box without finding the element. I mean, some way where send key can automatically look for focused inputbox and type input to it.

My code does not work obviously

driver.send_keys("testdata")
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Saurabh Shrivastava
  • 1,091
  • 1
  • 8
  • 8
  • If the element is active, you could just send individual keystorkes: http://stackoverflow.com/a/11507909/2374517 – cvakiitho Oct 01 '15 at 12:01

3 Answers3

58

Solved it

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(self.driver)
actions.send_keys('dummydata')
actions.perform()
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Saurabh Shrivastava
  • 1,091
  • 1
  • 8
  • 8
  • 5
    Use `actions = ActionChains(driver)` if you face issues on line 2 of the above answer. The author probably has the driver generated within a class object, hence needed self.driver to access it. – Ayush Mandowara Aug 26 '20 at 17:11
17

If you get error about 'self' in this code:

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(self.driver)
actions.send_keys('dummydata')
actions.perform()

just use:

actions = ActionChains(driver)

I don't have comment rights that's why I put this as answer

Edit: Added this enhancement as a comment on the original answer.

Ayush Mandowara
  • 490
  • 5
  • 18
5

This worked for me:

driver.find_element_by_tag_name('body').send_keys(' ')

(Which I used to use a space character to scroll through a page)

ErichBSchulz
  • 15,047
  • 5
  • 57
  • 61