Send key to browser (not element)

12,837 views
Skip to first unread message

BigSean

unread,
Feb 16, 2012, 6:11:12 PM2/16/12
to webdriver
Hi there,

I'm trying to write a test that verifies auto-focus behavior. That
is, when you click a button, a form pops open, and I want to make sure
the cursor has been configured to be typing in the first field.

I've tried the following code, but no luck:
btn.click();
Thread.sleep(2000);
new Actions(driver).sendKeys("The Title");

I've verified that the cursor is in the text field that I want to type
into when this command runs, but still no keys show up in the field.

I'm using the FIrefoxDriver with Firefox 10.0.1.

Is there anything else I can try?

Thanks!
-Sean

Danny Guerrier

unread,
Feb 17, 2012, 7:56:54 AM2/17/12
to webd...@googlegroups.com
You still need to find the element before you interact with it.

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>

Abhijeet Bedagkar

unread,
Feb 17, 2012, 8:52:32 AM2/17/12
to webd...@googlegroups.com
try this code worked for me

driver.SwitchTo().ActiveElement().SendKeys("Text");

this will put the text in the current active element.

Thanks
Abhijeet

Automation Engineer

Sean Adkinson

unread,
Feb 17, 2012, 11:09:45 AM2/17/12
to webd...@googlegroups.com
@Danny
The problem is that if I find the element and send keys to it, the focus changes to that element, so I can't verify it already had focus.

@Abhijeet
Thank you, that works pretty well.  I still wish there was a way to simply send keys, though, because the next thing I wanted to do was to press TAB and continue typing.  I can achieve this by doing the following:

driver.switchTo().activeElement().sendKeys("The Title" + Keys.TAB);

driver.switchTo().activeElement().sendKeys("The Description");

I was hoping I could just type ("The Title" + Keys.TAB + "The Description"), but this isn't too bad.


Thanks!

-Sean

Danny Guerrier

unread,
Feb 17, 2012, 7:05:22 PM2/17/12
to webd...@googlegroups.com
Maybe you can use the javaScript executor with 
document.activeElement

Never tried it. 
--

Simon Stewart

unread,
Feb 21, 2012, 9:38:09 AM2/21/12
to webd...@googlegroups.com
Try to send the keyboard events to the body element (find By.tagName("body"))

Simon

Chema del Barco

unread,
Feb 22, 2012, 1:21:11 PM2/22/12
to webdriver
Hey Simon,

Related to this... How would you do that for a pdf opened in a
browser? I tried every solution but none worked. I mean:
1) Opening a pdf in a browser
2) Press "Control" + "A" to get all the text
3) Press "Control" + "C" to copy the text to the clipboard

Thanks!

Simon Stewart

unread,
Feb 23, 2012, 6:49:49 AM2/23/12
to webd...@googlegroups.com
WebDriver doesn't support automating PDF viewers in browsers. If you
need to work with PDFs, the best thing is to download it to the local
machine (possibly by setting firefox to always download pdfs to a
known location) and then use a library suitable for working with that
document type.

Simon

Ramki Thirunahari

unread,
Apr 12, 2017, 2:20:28 AM4/12/17
to webdriver, sean.a...@gmail.com
Hi Sean,

Do you still have the working code of this? can you please share full code with me?

Thanks,
Ramki

Snehal Biche

unread,
Apr 12, 2017, 10:56:18 AM4/12/17
to webdriver, sean.a...@gmail.com
kind of this below works for me -

you need to wrap your driver to Action driver and use sendKeys this will actually dont check element is present or not.
(new Actions(driver)).sendKeys("ReplaceWithYourElement").perform();

-Snehal
Reply all
Reply to author
Forward
0 new messages