Skip to content
Advertisement

Select a button with selenium with same XPATH but no tag or id

I would like to select a button with selenium on the net, here’s the HTML code of the button:

<button type="button" ng-if="grid.appScope.edit_column" ng-click="grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])" class="btn btn-default ng-scope" aria-label="Left Align">
                <span class="glyphicon glyphicon-edit glyphicon-align-left" aria-hidden="true"></span>
        </button>

They are 1 to 5 and have all the same XPATH : //html/body/div[2]/div/section/section/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/div[2]/div/div[1]/div/button[1]

How can I select the third or second button with no tag in the HTML code?

Advertisement

Answer

driver.find_elements_by_xpath("//html/body/div[2]/div/section/section/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/div[2]/div/div[1]/div/button[1]")[2]

You can use find_elements instead of find_element. Then you can get the 2. index of the result.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement