Home > Software engineering >  Xpath: Need to select an xpath with changing div but same text
Xpath: Need to select an xpath with changing div but same text

Time:10-15

I am trying to use browser.find_element to select a 9:00 PM timeslot on this booking website.

Depending on the week the xpath for the 9:00 PM timeslot may be:

/html/body/div[3]/div[3]/div[4]/div[2]/div[1]/div[4]/table/tbody/tr/td[6]/div[13]

OR

/html/body/div[3]/div[3]/div[4]/div[2]/div[1]/div[4]/table/tbody/tr/td[6]/div[14]

The variable div (13 or 14 above) is as follows, which contains a div with 9:00 pm.

<div  style="height: 133px; cursor: pointer; width: 145px;" xpath="1">

<div > 9:00 pm</div>

</div>

How can I specify the xpath in a way that will always reference the 9:00 pm timeslot.

I tried this but it did not work:

browser.find_element("xpath", '/html/body/div[3]/div[3]/div[4]/div[2]/div[1]/div[4]/table/tbody/tr/td[6]/div[contains(text(), "9:00 pm")])')

Thank you for your help.

CodePudding user response:

Try the below xpath -

//div[contains(@class,'timeslot')]/div[contains(text(),'9:00 pm')]
  • Related