Nicolas Maslorz
Nicolas Maslorz

Reputation: 55

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?

Upvotes: 0

Views: 129

Answers (2)

Prophet
Prophet

Reputation: 33361

In case the ng-click attribute inside those buttons value is unique for each element you can use something like this:
//button[contains(@ng-click.'grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])')] or any unique part of grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options']) value.
If these values are same for all the 5 button elements you can use the following:
(//button[contains(@ng-click.'grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])')])[3] for the third element.
generally, you can put index from 1 to 5 at the end of this expression as it appears here.

Upvotes: 2

Client
Client

Reputation: 140

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.

Upvotes: 1

Related Questions