1

I have this piece of HTML code:

<li class="ui-menu-item" id="ui-id-12" tabindex="-1"><a>2</a></li>
<a>2</a>
</li>

The id "ui-id-12" is dynamically changing with the number being the part that is changed so I was wondering how I would go about finding this element using Selenium & XPaths. I was considering finding the element using the content within the anchor element tags but was not sure if this was smart so I have come here instead to ask what would be the best option.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Samrat Sahoo
  • 565
  • 8
  • 17

1 Answers1

1

The value of the id attribute of the <li> element i.e. ui-id-12 is dynamic. So to find the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element_by_css_selector("li.ui-menu-item[id^='ui-id-']")
    
  • Using xpath:

    element = driver.find_element_by_xpath("//li[starts-with(@id, 'ui-id-') and @class='ui-menu-item']")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352