13

In my tests I have a number of modals and dropdowns which open and automatically have focus set on their input boxes.

I have seen that it is possible to assert that an element is in focus. But is there any shortcut way to send keys to the element currently in focus? I'd like to be able to skip all the work of finding each element every time.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Niel
  • 1,856
  • 2
  • 23
  • 45

2 Answers2

14

You can use activeElement() feature of selenium to access the currently focused element source.

E.g.

// select the current page context's active element
browser.switchTo().activeElement().sendKeys('Test')
Community
  • 1
  • 1
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
7

Alternatively, you may use "browser actions":

browser.actions().sendKeys('Test').perform();
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195