-1

In the below XPath, the index [1] is changing every time. Can we handle this?

//button[contains(text(),'Action')][1]

HTML:

<button aria-expanded="false" aria-haspopup="true" class="dropdown-toggle component--button btn--reset" data-toggle="dropdown" type="button">
<span aria-hidden="true" class="icon component--icon icon-angle-down">
</span> Action</button>
  • Please add your HTML code to create XPath for the particular scenario. – Bharat Mane Jul 3 at 8:24
  • <button aria-expanded="false" aria-haspopup="true" class="dropdown-toggle component--button btn--reset" data-toggle="dropdown" type="button"><span aria-hidden="true" class="icon component--icon icon-angle-down"></span> Action</button> – Payal Jain Jul 3 at 8:32
  • //button[contains(text(),'Action')] - This should work. Which error are you getting? Which part is dynamic in your case? Is there the multiple buttons are available on the page with same text - Action? – Bharat Mane Jul 3 at 9:25
0

If there is only one element of tag "button" and text "action" then use:

//button[contains(text(),'Action')]

If, on the other hand, the above is true AND there are multiple instances of that button, AND you want the first one, surround it first with parenthesis, then the subscript:

(//button[contains(text(),'Action')])[1]

Not the answer you're looking for? Browse other questions tagged or ask your own question.