send keys to focused element

2,020 views
Skip to first unread message

Kamil Dworakowski

unread,
Aug 5, 2009, 5:18:02 AM8/5/09
to webdriver
Can webdriver send keys to whatever that has focus in the window?

Simon Stewart

unread,
Aug 5, 2009, 5:30:05 AM8/5/09
to webd...@googlegroups.com
Yes:

WebElement active = driver.switchTo().activeElement();
active.sendKeys("foobar");

Regards,

Simon

Kamil Dworakowski

unread,
Aug 5, 2009, 7:09:03 AM8/5/09
to webdriver


On Aug 5, 10:30 am, Simon Stewart <simon.m.stew...@gmail.com> wrote:
> Yes:
>
> WebElement active = driver.switchTo().activeElement();
> active.sendKeys("foobar");

Thx. It was not exposed through the Python api. Here is a patch for
firefox/src/py/webdriver.py

@@ -49,7 +49,7 @@
def execute_script(self, script, *args):
"""Executes arbitrary javascript.
For WebElement argument, the format is:
- execute_script("argument[0].value='cheese'", elem)
+ execute_script("arguments[0].value='cheese'", elem)
"""
converted_args = []
for arg in args:
@@ -170,6 +170,15 @@
def find_elements_by_tag_name(self, tag_name):
"""Finds all the elements with the given tag"""
return self._find_elements_by("TagName", tag_name)
+
+ def switch_to_active_element(self):
+ """Returns the focused element"""
+ return WebElement(self, self._command
("switchToActiveElement"))
+
+ def send_keys(self, keys):
+ """Types to the focused element"""
+ focused_element = self.switch_to_active_element()
+ focused_element.send_keys(keys)

def get_page_source(self):
"""Gets the page source."""


Cheers,
Kamil
Reply all
Reply to author
Forward
0 new messages