Finding an element whose Xpath is unique to each login

245 Views Asked by At

I am coding a python web automation selenium script.

In the script, I use driver.find_element_by_xpath('xpath') to find elements on Binary.com. This means I would have to preload Binary.com and copy xpaths of the elements I need to find. For most elements the method works but for a few I realised that the Xpath is unique to each login.

For example, if I login in now and try to copy the xpath of a certain element it will be //*[@id="tp1602844250562"] but if the page is reloaded or I try to login on a different window the xpath would have then changed to //*[@id="tp1602844157070"]. Please note they are not the same id numbers. This means I cannot use one xpath on a separate page login

The desired element has an HTML code:

<input type="text" class="time hasTimepicker" tab-index="-1" value="00:00" readonly="" id="tp1602844157070">

Refer to the supplied image for clear html codehtml code

4

There are 4 best solutions below

0
On BEST ANSWER

You can try below with class instead of id as the id is getting pulled from DB i guess-

//div[@class='date-time']//input[contains(@class,'time')]
1
On

Why don't you use the class instead of the id? Try this xpath:

driver.find_element_by_xpath('//input[@class = "time hasTimepicker"]')
0
On

Try changing your xpath expression to

//input[starts-with(@id,"tp")]

or, if it's not always input

//*[starts-with(@id,"tp")]
0
On

To find the input element with that class use.

driver.find_element_by_css_selector("input.time.hasTimepicker")