xpath for sibling element

1,584 views
Skip to first unread message

Davina Armstrong

unread,
Apr 7, 2010, 1:25:09 PM4/7/10
to Selenium Users
I have a checkbox in my app with a label preceeding it. I need to
figure out an xpath locator for the checkbox based on the label text.
I'm not sure how to get to it. The html is

<tr><td width="110px"><div class="gwt-Label">Group By:</div></td><td
width="5px"></td><td><span class="gwt-CheckBox foo-check-box"
style="width: 100%;"><input type="checkbox" id="gwt-uid-233"
tabindex="0"><label for="gwt-uid-233"></label></span></td></tr>

I'm trying to click on the checkbox next to the label "Group By".

I'm starting to get the hang of this selenium thing, but still need
some help!

Thanks,
Davina

Ross Patterson

unread,
Apr 7, 2010, 1:45:56 PM4/7/10
to seleniu...@googlegroups.com
This ought to work. It means "the <td><span class='gwt-CheckBox'><input type='checkbox'> contained within the <tr> that contains <td><div class='gwt-Label'>Group By"

xpath=//tr[td/div[@class='gwt-Label' and text() = 'Group By']/td/span[@class='gwt-CheckBox']/input[@type='checkbox']

You can probably simplify that a bit, to:

xpath=//tr[td/div/text() = 'Group By']//input[@type='checkbox']

which means "the <input type='checkbox'> contained somewhere within the <tr> that contains <td><div>Group By"

But if the checkbox's id (gwt-uid-233) is repeatable, as these things often are, it would be simpler to use it directly:

id=gwt-uid-233

Ross Patterson

Thanks,
Davina

--
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.

ds tara

unread,
Apr 7, 2010, 1:58:58 PM4/7/10
to seleniu...@googlegroups.com
Worked like a charm!  Thanks so much, Ross!  (BTW, that ID isn't consistent.  I'm working with our devs to get static ids on on all our widgets, but in the meantime...)

Mark Collin

unread,
Apr 7, 2010, 4:13:46 PM4/7/10
to seleniu...@googlegroups.com
I would suggest:

//label[.='Group By']/preceeding-sibling::input

Regards

Mark

-----Original Message-----
From: seleniu...@googlegroups.com
[mailto:seleniu...@googlegroups.com] On Behalf Of Davina Armstrong
Sent: 07 April 2010 18:25
To: Selenium Users
Subject: [selenium-users] xpath for sibling element

Thanks,
Davina

--

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.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Michael Hwee

unread,
Apr 7, 2010, 2:00:31 PM4/7/10
to seleniu...@googlegroups.com
"//div[text()='Group By:']//input[@id='gwt-uid-233']"


----- Original Message ----
From: Davina Armstrong <dstar...@gmail.com>
To: Selenium Users <seleniu...@googlegroups.com>
Sent: Wed, April 7, 2010 10:25:09 AM
Subject: [selenium-users] xpath for sibling element

Thanks,
Davina

--

Ritesh

unread,
Apr 8, 2010, 8:38:46 AM4/8/10
to Selenium Users
Hi,

If you have multiple rows,and all are following same pattern like you
have shown in your code

<tr>
<td width="110px">
<div class="gwt-Label">Group By:</div>
</td>

<td width="5px">
</td>

<td>
<span class="gwt-CheckBox foo-check-box" style="width: 100%;">
<input type="checkbox" id="gwt-uid-233" tabindex="0">
<label for="gwt-uid-233">
</label>
</span>
</td>
</tr>

You can use following approach

int count=1;
while(selenium.isElelmentPresent(//tr["+count+"])) //you have to
specify xpath up to row, here I have used only tr
{
if(selenium.isElementPresent(//tr["+count+"]/td/div[text()='Group
By:']))
{
selenium.click((//tr["+count+"]/td[3]/span/input);
break;
}
count++;
}

Let me know If this approach will not work.

Thanks
Ritesh

Mark Collin

unread,
Apr 8, 2010, 8:58:39 AM4/8/10
to seleniu...@googlegroups.com
In my opinion you are vastly overcomplicating things here.

The code snippet you have supplied will programmatically iterate through
each row in the table until it finds a row that matches the criteria. At
this point iw ill click on the input and break out of the loop.

You could do exactly the same thing with one line of code:

selenium.click("//tr[td/div[.'Group By:']]/td[3]/span/input");

or if you want to check the element is there first:

if (selenium.isElementPresent("//tr[td/div[.'Group
By:']]/td[3]/span/input")){
selenium.click("//tr[td/div[.'Group By:']]/td[3]/span/input");
}

> --
> 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.
>
>

Reply all
Reply to author
Forward
0 new messages