Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XPath to element with changing id?

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)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

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

Append whatever invariant suffix is needed from there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading