How to change focus?

135 views
Skip to first unread message

Jack

unread,
May 2, 2008, 12:53:11 PM5/2/08
to webdriver
Hi,

One page have a selection and a button.
You have to first click the button to see the selection.
Following code does not work:

button= driver.findElement(By.xpath("//input[@class='loginButton']"));
button.click();

select = driver.findElement(By.xpath("//select"));
List<WebElement> options = select.getChildrenOfType("option");
for (WebElement option : options) {
if ("option1".equalsIgnoreCase(option.getValue( ))) {
option.setSelected();
break;
}
}

button.click();

The second button.click() will be triggered on select element. It
seems that the focus is not changed to button.
Right now a temporary solution is to get the button reference again.

One post said there is element.setfocus(), but it seems my binary
build don't have this. How to change focus?

Thanks,
Jack

Simon Stewart

unread,
May 7, 2008, 2:47:51 AM5/7/08
to webd...@googlegroups.com
Hi Jack,

The focus should be on the element that's being used. The firefox
driver calls "element.focus()" before executing a click as well as
firing the mousedown and mouseup events, so that should work as
expected. Are you using the released firefox binaries?

Regards,

Simon

Vinay Seth Mohta

unread,
Jul 3, 2008, 11:43:35 PM7/3/08
to webdriver
Just started using WebDriver and really like your design choices!
Thank you for creating WebDriver!

On a related note to this thread, I'm seeing something similar and
wondered if I'm doing something wrong:

driver.get("http://localhost:8500/test/discussion/create/");
driver.findElement(By.id("id_keyword_list")).click();
driver.findElement(By.id("id_keyword_list")).sendKeys("mal");

id_keyword_list is an input field that has some Javascript methods
tied to it so that on initial page load, the field shows an input
hint, an onfocus callback clears the input field so that the user can
type in a value, and an onblur callback checks if the input field is
empty and if it is, puts the hint back in.

I would expect that the above click() call would result in the onfocus
callback being called, as you mention above; however, when I look at
the browser after the sendKeys event, the string 'mal' has been
appended to the input hint, implying that the onfocus callback was
never called.

If I call clear() instead of click(), everything works as expected.

I'm using the current SVN checkout of webdriver and using FF 2.0.15

Thanks,
Vinay



On May 7, 2:47 am, "Simon Stewart" <simon.m.stew...@gmail.com> wrote:
> Hi Jack,
>
> The focus should be on the element that's being used. The firefox
> driver calls "element.focus()" before executing a click as well as
> firing the mousedown and mouseup events, so that should work as
> expected. Are you using the released firefox binaries?
>
> Regards,
>
> Simon
>

Simon Stewart

unread,
Jul 4, 2008, 5:28:07 AM7/4/08
to webd...@googlegroups.com
Hi Vinay,

Looking at the code for the Firefox driver[1] we do call focus on the
element before firing the click event, though not blur. We may need to
allow control to flow away from us before continuing with the other
events. I'll try and put a test case together for this.

As an aside, Firefox only reliably fires focus events when its window
is the active one. It's possible that may be causing you trouble,
though I doubt it in this case; it's probably webdriver not doing the
Right Thing :)

Regards,

Simon

Simon Stewart

unread,
Jul 4, 2008, 7:06:43 AM7/4/08
to webd...@googlegroups.com
Hi,

Would you mind trying the newest version? I've patched the Firefox and
IE drivers, so hopefully things will now be better.

Regards,

Simon

Reply all
Reply to author
Forward
0 new messages