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
  • 8

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,231
  • 1
  • 7
  • 23
  • 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
  • 20,849
  • 6
  • 20
  • 43
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,488
  • 17
  • 26
0

To find the input element with that class use.

driver.find_element_by_css_selector("input.time.hasTimepicker")
Arundeep Chohan
  • 8,523
  • 4
  • 9
  • 28