Leo
Leo

Reputation: 189

Get the last occurance of the an object

In my html page there is an option to add multiple "Resource Identity". When I add a new one, it it creates a new row with the following elements.

<tr>
<td width="150" valign="top">
<input type="hidden" name="expressions[0].checked"/>
<input type="hidden" value="B" name="expressions[0].expressionLabel"/>
<b>Resource Identity</b>
<input type="hidden" value="ResourceIdentity" name="expressions[0].parameterName"/>
<input type="hidden" value="ResourceIdentity" name="expressions[0].fieldName"/>
<input type="hidden" value="RF" name="expressions[0].fieldType"/>
<input type="hidden" value="" name="expressions[0].limitedValues"/>
</td>
</tr>


<tr>
<td width="150" valign="top">
    <input type="hidden" name="expressions[1].checked"/>
    <input type="hidden" value="B" name="expressions[1].expressionLabel"/>
    <b>Resource Identity</b>
    <input type="hidden" value="ResourceIdentity" name="expressions[1].parameterName"/>
    <input type="hidden" value="ResourceIdentity" name="expressions[1].fieldName"/>
    <input type="hidden" value="RF" name="expressions[1].fieldType"/>
    <input type="hidden" value="" name="expressions[1].limitedValues"/>
    </td>
    </tr>

How do I find the last occurrence of <b>Resource Identity</b>? I am trying to find out the name above the tag by using the below xpath, but it always evaluates to the find occurance as selenium scans the html from top to bottom.

driver.findElement(By.xpath(//b[text() = 'Resource Identity']/preceding-sibling::input[1])).getAttribute("name").toString();

If there are multiple "Resource Identities", then how do I go to the last "Resource Identity" and find the name?

Upvotes: 0

Views: 740

Answers (1)

alecxe
alecxe

Reputation: 474003

Use last():

(//b[text() = 'Resource Identity']/preceding-sibling::input[1])[last()]

Upvotes: 1

Related Questions