0

I have a HTML like below:

<button id = "btn-89sd788" ng-click="ctrl.seeDetail()" aria-label = "go to item 1222" class="btn btn-green ng-binding" xpath="1"> 
"view details"
</button>

the form has multiple buttons with similar html code, the button id is different but the aria-label text is same with the item number(in this case 1222) ibeing incremental. I want to loop clicking on the buttons based on the aria label. How do I find the xpath for this button.

zx485
  • 28,498
  • 28
  • 50
  • 59

2 Answers2

1

Is this what you're looking for?

//button[@aria-label="go to item 1222"]

This will find all buttons with the tag aria-label equal to "go to item 1222"

In order to iterate through them, you could do something like: (Python)

for i in range(num_buttons):
  xpath = '//button[@aria-label="go to item "' + i + ']'

Peter
  • 848
  • 8
  • 14
  • yes exactly. It worked. Thanks. I am newbie with selenium and struggling in finding xpath – trickspring Apr 21 '20 at 20:23
  • No problem. Selenium is a great library and xpaths are very easy to learn. I use www.xpather.com to help me locate elements and test xpaths. Very useful. – Peter Apr 21 '20 at 20:25
1

Try this XPath-1.0 expression to choose the <button>s incrementally:

//button[contains(@aria-label,'go to item')]

This expression selects all <button>s that satisfy the condition.

zx485
  • 28,498
  • 28
  • 50
  • 59