Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Xpath how to get the value from a node above

I have the following html structure. The input id value is dynamic. E.g. gwt-uid-150 I am trying to work out an Xpath to get radio button labelled "Comma" so i can click it.

<table class="gwt-DisclosurePanel gwt-DisclosurePanel-open" cellspacing="0" cellpadding="0">
<tbody>
    <tr>
    <tr>
        <td align="left" style="vertical-align: top;">
            <div style="padding: 0px; overflow: hidden;" aria-hidden="false">
                <div class="content" aria-hidden="false">
                    <span class="gwt-RadioButton block">
                        <input id="gwt-uid-150" type="radio" name="delimiter" value="on" tabindex="0" checked=""/>
                        <label for="gwt-uid-150">Comma</label>
                    </span>
                    <span class="gwt-RadioButton block">
                    <span class="gwt-RadioButton inline marginright">
                    <input class="gwt-TextBox" type="text" disabled="" size="1" maxlength="1"/>
                </div>
            </div>
        </td>
    </tr>
</tbody>
</table>

If i use the following Xpath I can get the label

//label[contains(text(), "Comma")]

I would like to get the node above it which is the input id of type radio, as this is the radio button.

I tried using ancestor::

//label[contains(text(), "Comma")]/ancestor::

How do i do it please?

Thanks, Riaz

Upvotes: 1

Views: 1275

Answers (1)

alecxe
alecxe

Reputation: 473863

Yeah, getting it by label is a good approach here, use preceding-sibling:

//label[contains(., "Comma")]/preceding-sibling::input

Upvotes: 1

Related Questions