XPath to element with changing id?

Viewed 178

I am trying to locate one element using Selenium with XPath. When I inspect the same element and copy the XPath, it is

//*[@id="Body react-aria-**16**"]/div/div/div[1]/ul/li[2]/div

However, the number "16" may change every time I load the page. How can I reliably locate this element using find_element(By.XPATH, XPath)?

1 Answers

To eliminate dependence on the trailing number, use starts-with():

//*[starts-with(@id,"Body react-aria-")]

Append whatever invariant suffix is needed from there.

Related