Kevin de Boer
Kevin de Boer

Reputation: 115

Complicated xpath for a rookie

I have this piece of html:

<tr>
  <td class="has-checkbox">
    <input id="abc" class=... value=...>
  </td>
  <td class="has-label">
    <label for="abc">Name1234</label>
  </td>
<tr>

I need to make an xpath that gets me the input element, based on whats in the label, in this case Name1234.

In other words, for this case, I need an xpath to the input element, and the path must contain Name1234, as its variable.

Anyone who can help me out here?

Upvotes: 0

Views: 608

Answers (4)

Đỗ Quang Khải
Đỗ Quang Khải

Reputation: 77

You can use /.. , this syntax use to move back to parent node. In your case:

//label[.='Name1234']/../../td/input

You must move back 2 times because input tag is the child of another td tag.

Here are others introduction and example about you should read.

Upvotes: 1

Tomas Chudjak
Tomas Chudjak

Reputation: 562

It's not so complicated as you think:

xpath=//tr[//label[.="Name1234"]]//input

in other words, you are looking for the 'tr' which contains 'label' with text "Name1234". If the condition is true, you are getting the 'input' element

Upvotes: 0

rishimaharaj
rishimaharaj

Reputation: 1706

Here is a solution using the Axes parent and preceding-sibling:

//label[.='Name1234']/parent::td/preceding-sibling::td/input

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167706

//input[@id = //label[. = 'Name1234']/@for] selects input element(s) with an id attribute value equal to the for attribute value of label elements where the contents is Name1234.

Upvotes: 2

Related Questions