Esteban Balvin
Esteban Balvin

Reputation: 117

XPath selector for dynamic ID

I have been trying to find a good XPath selector for the below HTML, there is a dynamic ID. I need to select in my Selenium IDE the option from that list 02 but it fails because the ID is dynamic.

Please help!!!

<select class="c-select__input c-select" id="uf5rwnhtsy" required="">
  <option class="c-select__option" value="##">HH</option>
  <option class="c-select__option" value="01">01</option>
  <option class="c-select__option" value="02">02</option>
  <option class="c-select__option" value="03">03</option>
  <option class="c-select__option" value="04">04</option>
  <option class="c-select__option" value="05">05</option>
  <option class="c-select__option" value="06">06</option>
  <option class="c-select__option" value="07">07</option>
  <option class="c-select__option" value="08">08</option>
  <option class="c-select__option" value="09">09</option>
  <option class="c-select__option" value="10">10</option>
  <option class="c-select__option" value="11">11</option>
  <option class="c-select__option" value="12">12</option>
</select>

Upvotes: 0

Views: 60

Answers (2)

Esteban Balvin
Esteban Balvin

Reputation: 117

I resolved it by

xpath=//select[contains(@class, 'c-select__input c-select')]/option[8]

Upvotes: 0

Shnigi
Shnigi

Reputation: 1012

Use classname

//*[contains(@class, 'c-select__input c-select')]

Or be more specific

//select[contains(@class, 'c-select__input c-select')]

Or if you can't use class name you have to start upper from the tree, select something that is not changing and use the xpath to go down the tree until you hit the select component. For example consider the following:

//*[@id="hello"]//select

Or if you have access to the source code just add selector that is not changing. For example

data-testautomation="myElement"

//*[@data-testautomation="myElement"]

Upvotes: 1

Related Questions