WebDriver: Set Focus on Button like Selenium.focus()

25,564 views
Skip to first unread message

Arun Kumaresan

unread,
Jan 16, 2012, 9:45:16 PM1/16/12
to Selenium Users
Hi,
How do I set the focus on a button using WebDriver.
I am using WebDriver 2.16.0.

Is there an API anologus to selenium.focus(<Element>);

I realized that sometimes clicking on IE using webdriver doesnt work
but it works if I set the focus on the button.

As a Crude way, if you call webdriver.click(<ButtonId>) twice (Incase
of IE), the 1st click does a focus on the button and the second call
to click API actually clicks the button. But I cannot depend on this
workaround as the buttons that dont need click twice will fail.

So is there a right way to set the focus on a button using WebDriver.

Any kind of help is greatly appreciated.

Thanks.
Arun Kumaresan.

Arun Kumaresan

unread,
Jan 16, 2012, 9:49:31 PM1/16/12
to Selenium Users
In my previous email, I meant to say that, the Issue "Clicking a button on IE using Webdriver" gets resolved, if the focus on the button is set before you call the click API on the button using webdriver. But the Issue is how to set the focus on the button.
 
Pls help.
--
Regards,
Arun Kumaresan

sanam

unread,
Mar 14, 2012, 4:36:46 AM3/14/12
to Selenium Users
Hi

I guess your issue must have been resolved by now as this is an old
post..

In webdriver , the focus is implicitly set on the objects before
performing an action. However in your case you can execute the js code
like is

js.executeScript ("document.getElementById('"your button
id"').focus()")

hope this helps.

Thanks,
Sanam

On Jan 17, 7:49 am, Arun Kumaresan <arunkumare...@gmail.com> wrote:
> In my previous email, I meant to say that, the Issue "Clicking a button on
> IE using Webdriver" gets resolved, if the focus on the button is set before
> you call the click API on the button using webdriver. But the Issue is how
> to set the focus on the button.
>
> Pls help.

Jayaraman - Inspired to become Software Architect

unread,
Mar 14, 2012, 6:23:50 AM3/14/12
to seleniu...@googlegroups.com
Before clicking the button, you can set the focus by the following way.
 
driver.findElement(By.Id(Id of the button)).sendKeys(Keys.Tab)
 
This is one of the possible solution and Hope this helps you.
 
-Jay!!

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

sanam

unread,
Apr 2, 2012, 2:47:41 AM4/2/12
to Selenium Users
Hi Jay!!

I tried your solution on Google Chrome, IE and FF. However,
it was behaving as follows:

driver.findElement(By.Id(Id of the button)).sendKeys(Keys.Tab)

It gets the button with the given id and then sends a tab to it
resulting in setting the focus on the next element.

How did this work at your end?

Thanks,
Sanam

On Mar 14, 6:23 am, Jayaraman - Inspired to become Software Architect
<electrical.i...@gmail.com> wrote:
> Before clicking the button, you can set the focus by the following way.
>
> *driver.findElement(By.Id(Id of the button)).sendKeys(Keys.Tab)*
>
> This is one of the possible solution and Hope this helps you.
>
> -Jay!!
> On Tue, Jan 17, 2012 at 8:19 AM, Arun Kumaresan <arunkumare...@gmail.com>wrote:
>
>
>
>
>
>
>
> > In my previous email, I meant to say that, the Issue "Clicking a button on
> > IE using Webdriver" gets resolved, if the focus on the button is set before
> > you call the click API on the button using webdriver. But the Issue is how
> > to set the focus on the button.
>
> > Pls help.

Arun Kumaresan

unread,
May 22, 2012, 11:32:50 AM5/22/12
to seleniu...@googlegroups.com
Yes that is what I am doing now.
Unfortunately some of the buttons that I have to click on dont have a "id".
So I had to find a way to get to the xpath(Using dojo.query) of the button and do a .focus() on it.

Anyways, I have got a workaround now.

Thanks.

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




--
Regards,
Arun Kumaresan
Reply all
Reply to author
Forward
0 new messages