Divya Karumuri
Divya Karumuri

Reputation: 11

XPath of element preceding some text

Trying to find the XPath for this code:

<a class="account-name" long-text="Sample Text" ng-
click="accountClick(account)" ng-href="sample URL" href="sample URL1">
<span>Plan Name</span></a>

I've tried this:

//span[text()='Plan Name']

and it doesn't seem to be working.

Upvotes: 0

Views: 51

Answers (1)

kjhughes
kjhughes

Reputation: 111726

For this HTML,

<a class="account-name" long-text="Sample Text" ng-
click="accountClick(account)" ng-href="sample URL" href="sample URL1"/>
<span>Plan Name</span>

This XPath,

//span[.="Plan Name"]/preceding-sibling::a[1]

will selecting the immediately preceding a elements to span elements whose string value is "Plan Name".


For this HTML,

<a class="account-name" long-text="Sample Text" ng-
click="accountClick(account)" ng-href="sample URL" href="sample URL1">
  <span>Plan Name</span>
</a>

This XPath,

//a[.="Plan Name"]

will select all a elements with string values equal to "Plan Name".

Upvotes: 1

Related Questions