Search code examples
selenium-webdriverxpathfindelement

How to find xpath for a label inside a button tag


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.


Solution

  • 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 + ']'