0

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

Moshe
  • 107
  • 1
  • 9

4 Answers4

2

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

driver.find_element_by_xpath('//input[@class = "time hasTimepicker"]')
Sushil
  • 5,440
  • 1
  • 8
  • 26
  • This is not the best way to filter on class names. Please have a look at this answer https://stackoverflow.com/questions/1604471/how-can-i-find-an-element-by-css-class-with-xpath/1604480#1604480. – gurisko Oct 17 '20 at 08:40
2

Try changing your xpath expression to

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

or, if it's not always input

//*[starts-with(@id,"tp")]
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
2

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')]
JRazor
  • 2,707
  • 18
  • 27
0

To find the input element with that class use.

driver.find_element_by_css_selector("input.time.hasTimepicker")
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32