Element focus

16 views
Skip to first unread message

Penelope

unread,
Sep 3, 2009, 5:44:23 PM9/3/09
to webdriver
Hi,

I am trying to find all form elements on a page and then loop through
them. In this example, I'm picking out First Name and Last Name as
elements I care about, and sending text to them.

For some reason, the iterator is not giving me focus. If I used the
lines that are commented out (find by name), it works just fine. I
would like it to be more abstract, though. (I will eventually remove
the "First Name" and "Last Name" matching and use something more
generic and/or random.)

Here's the code:
List<WebElement> form_elements = driver.findElements(By.xpath("//label
[@*]"));
for (WebElement form_elem : form_elements){
System.out.println("form query results: " + form_elem.getText
());
if (form_elem.getText().equals("First Name")){
// driver.findElement(By.name
("ctlStartRez:txtFirstName")).sendKeys("Foo");
form_elem.sendKeys("Foo");
} else if (form_elem.getText().equals("Last Name")){
// driver.findElement(By.name
("ctlStartRez:txtLastName")).sendKeys("Bar");
form_elem.sendKeys("Bar");
}
}

Here's the error I get:
form query results: First Name
We don't have focus on element.
form query results: Last Name
We don't have focus on element.

I'm sure it's a silly mistake but I just don't see it.

thanks,
Penelope

Steve CCRP

unread,
Sep 3, 2009, 6:28:19 PM9/3/09
to webdriver
I'm short on time, so I can't give the best answer at the moment. But
your code appears to be looping over the label elements and sending
keys to the label elements. You probably intended to do a sendKeys to
the <input> elements. In other words, you are typing keys on the
labels instead of in the text box.

I'm sure if you paste a bit of the html, myself (later on) or others
can help with how to accomplish what you want to do.

-Steve

Daniel Wagner-Hall

unread,
Sep 3, 2009, 6:35:44 PM9/3/09
to webd...@googlegroups.com
Yeah, it sounds like you're sending keys to a label...

I would suggest using the for attribute of labels, something like (untested):

List<WebElement> form_elements = driver.findElements(By.xpath("//label[@*]"));
for (WebElement form_elem : form_elements){
System.out.println("form query results: " + form_elem.getText());
if (form_elem.getText().equals("First Name")){
driver.findElement(By.id(form_elem.getAttribute("for"))).sendKeys("Foo");
} else if (form_elem.getText().equals("Last Name")){
driver.findElement(By.id(form_elem.getAttribute("for"))).sendKeys("Bar");
}
}

If you're going to be doing this a lot, it may make sense to make
yourself a new kind of WebElement, which you could construct from the
result of driver.findElement(...), and for which sendKeys is overriden
to do the above.

Penelope

unread,
Sep 4, 2009, 9:08:06 AM9/4/09
to webdriver
Thanks! I'll try this. Still in proof of concept phase, so I won't be
creating new WebElements just yet. :) I really appreciate your help!

On Sep 3, 6:35 pm, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> Yeah, it sounds like you're sending keys to a label...
>
> I would suggest using the for attribute of labels, something like (untested):
>
> List<WebElement> form_elements = driver.findElements(By.xpath("//label[@*]"));
> for (WebElement form_elem : form_elements){
>   System.out.println("form query results: " + form_elem.getText());
>   if (form_elem.getText().equals("First Name")){
>     driver.findElement(By.id(form_elem.getAttribute("for"))).sendKeys("Foo");
>   } else if (form_elem.getText().equals("Last Name")){
>     driver.findElement(By.id(form_elem.getAttribute("for"))).sendKeys("Bar");
>   }
>
> }
>
> If you're going to be doing this a lot, it may make sense to make
> yourself a new kind of WebElement, which you could construct from the
> result of driver.findElement(...), and for which sendKeys is overriden
> to do the above.
>

Penelope

unread,
Sep 4, 2009, 9:59:43 AM9/4/09
to webdriver
It turns out I actually need to do my looping on "input" elements
instead of the labels...Which makes sense I guess. :) Now onto how to
select a value in a drop down menu.

Thanks again for your help!

On Sep 3, 6:35 pm, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> Yeah, it sounds like you're sending keys to a label...
>
> I would suggest using the for attribute of labels, something like (untested):
>
> List<WebElement> form_elements = driver.findElements(By.xpath("//label[@*]"));
> for (WebElement form_elem : form_elements){
>   System.out.println("form query results: " + form_elem.getText());
>   if (form_elem.getText().equals("First Name")){
>     driver.findElement(By.id(form_elem.getAttribute("for"))).sendKeys("Foo");
>   } else if (form_elem.getText().equals("Last Name")){
>     driver.findElement(By.id(form_elem.getAttribute("for"))).sendKeys("Bar");
>   }
>
> }
>
> If you're going to be doing this a lot, it may make sense to make
> yourself a new kind of WebElement, which you could construct from the
> result of driver.findElement(...), and for which sendKeys is overriden
> to do the above.
>

Simon Stewart

unread,
Sep 4, 2009, 10:14:15 AM9/4/09
to webd...@googlegroups.com
Assuming the drop down menu is a SELECT tag and you've got the
webdriver-support.jar, try this:

WebElement rawSelect =
driver.findElement(By.name("my-select"));
Select select = new Select(rawSelect);

You can then call methods on "select", which are hopefully clear. If
you've not got the support package, then you can do this:

WebElement rawSelect =
driver.findElement(By.name("my-select"));
List<WebElement> allOptions =
rawSelect.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
// Select this option
option.setSelected();
}

Regards,

Simon
Reply all
Reply to author
Forward
0 new messages