Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send keys to page without an input element

On a web page that has a keypress event listener running I want to be able to test sending keystrokes. But the page doesn't have an input element, and calling

driver.FindElement(By.XPath("//body")).SendKeys("abc");

Throws an error about needing the element to be editable to accept key strokes (against Chrome).

Is there any way to generate the keystrokes so the page sees them without injecting a dummy input element?

like image 693
Rob Walker Avatar asked May 10 '13 03:05

Rob Walker


1 Answers

I haven't tested it but would you mind give Actions.SendKeys a try?

Example is in C#:

// without an element
new Actions(driver).SendKeys("abc").Perform();

// send keys to body
new Actions(driver).SendKeys(driver.FindElement(By.XPath("//body")), "abc").Perform();
like image 72
Yi Zeng Avatar answered Oct 16 '22 23:10

Yi Zeng