Aaron Lelevier
Aaron Lelevier

Reputation: 20838

XPath how to find second element with CSS class name

I have this html:

<div tabindex="0" class="ember-basic-dropdown-trigger ember-power-select-trigger ember-power-select-trigger-2568211667 category-select t-ticket-category-select-trigger">
   Building
</div>

<div tabindex="0" class="ember-basic-dropdown-trigger ember-power-select-trigger ember-power-select-trigger-6101225577 category-select t-ticket-category-select-trigger">
  Warehouse
</div>

I would like to access the text in the 2nd div that has the same CSS class name, but am not sure how to access it.

I have some XPath code like this, but this doesn't work:

//*[contains(@class, 't-ticket-category-select-trigger')]

Upvotes: 2

Views: 2634

Answers (1)

alecxe
alecxe

Reputation: 474201

Just access it by index (indexing starts with 1 in XPath):

(//*[contains(@class, 't-ticket-category-select-trigger')])[2]

Or, if it is the last one:

(//*[contains(@class, 't-ticket-category-select-trigger')])[last()]

Upvotes: 3

Related Questions