Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help finding xpath of input field beside label

Tags:

selenium

xpath

I have a dom structure like this

<tr class="">
<td class="labelCol requiredInput">
    <label for="name_lastacc2" class="">
        <span class="requiredMark">*</span>
    Last Name</label>
</td>
<td class="dataCol col02">
    <div class="requiredInput">
        <div class="requiredBlock"></div>
        <input id="name_lastacc2" maxlength="80" name="name_lastacc2" size="20" tabindex="3" type="text"></div>
    </td>
    <td class="labelCol">
        <label for="00NG0000008Rybp">Business Ext.</label>
    </td>
    <td class="dataCol">
        <input id="00NG0000008Rybp" maxlength="10" name="00NG0000008Rybp" size="20" tabindex="22" type="text"></td>
    </tr>

I am trying to find the input box next to its label.

Eg:

Last Name: ________________

I have tried using xpath queries like

//label[contains(text(),'Last Name']/../following-sibling::*/div/input[@type='text'] 

but i get an [INVALID XPATH EXPRESSION] error. Where am i going wrong ?

like image 295
Enihr Avatar asked Oct 21 '25 14:10

Enihr


1 Answers

Your path expression is missing the closing bracket of contains().

//label[contains(text(),'Last Name')]/../following-sibling::*/div/input[@type='text']

But that said, I do not think your path expression is correct now. Your expression will match any following sibling of the parent of label[contains(text(),'Last Name')]. What you want it to find is the input element that immediately follows.

The only reason your original expression only finds one input element is that the second one is nested inside one more div. But I do not think you should rely on that.

Instead, try:

//label[contains(.,'Last Name')]/following::input[1]

Then, the result is

<input id="name_lastacc2" maxlength="80" name="name_lastacc2" size="20" tabindex="3" type="text"/>
like image 194
Mathias Müller Avatar answered Oct 23 '25 08:10

Mathias Müller