1

Very new to Selenium and Python (any form of coding in general). Trying to automate a bot to book a gym slot as they are booking up before I can even get a chance.

I'm trying to get selenium to click on the evening slot. The whole box is selectable (https://i.stack.imgur.com/rHdLf.png) (https://i.stack.imgur.com/vYxyk.png)

I tried using this

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class='activity-start-time ng-star-inserted')]//span[contains(text(),'5:00PM')]"))).click()

but it fails receiving the following exception: InvalidSelectorException. Unable to locate an element with the xpath expressions

Amin M
  • 13
  • 3

1 Answers1

0

InvalidSelectorException indicates that the xpath is a bit off and errorprone. As per the snapshot the <section> tag is having the classes activity-start-time ng-star-inserted and the text 5:00PM


Solution

You can use the following locator strategy:

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//section[@class='activity-start-time ng-star-inserted' and contains(.,'5:00pm')]"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks, I tried your locator strategy however it bounces back with an invalid selector exception message. Unable to locate an element with the xpath expression `//section[@class='activity-start-time ng-star-inserted'] and [contains(.,'5:00PM')] ` because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string `''//section[@class='activity-start-time ng-star-inserted'] and [contains(.,'5:00PM')]' ` is not a valid XPath expression. I'm at a bit of a loss here and can't seem to figure out why the xpath isn't working – Amin M Feb 07 '23 at 16:09
  • @AminM There was abug in the code. Corrected now. Please retest. – undetected Selenium Feb 07 '23 at 16:27
  • I've tried the new code once again but it seems to bounce back with the same Invalid Selector Exception. The Syntax Error ''Failed to execute evaluate on Document'' keeps occurring. My understanding is that the section `//section[@class` is not actually being found by Xpath so it can't search for the text ''5:00PM''. I tried some alternatives such as using `//div[contains(@class='activity-view-container ng star inserted')` to see if it can capture the element above the section that contains 5:00pm but it also gives the same invalidselectorexception – Amin M Feb 08 '23 at 01:58
  • 1
    I managed to get it to work. Not sure what I was doing wrong entering the code or copying and pasting but it now correctly selects it. Thank you for your help! – Amin M Feb 08 '23 at 02:13