Search code examples
htmlseleniumwebdriverclick

How to click on this web element using selenium webdriver?


HTML

How would you click on this particular item in selenium each time by having just the date you want as a variable? I have shown the item in the attached image of the HTML. It is a panel calendar, however, I can't find the item using by.ID, class, etc. I can Click on it by using XPath however how do I make it specific to the data? I would like to click on it based on the.

data-moment="2022-10-30"

currently I just have this:

### Gets Edge driver and doesnt need extension update
driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))
driver.get(
    'https://teewire.net/granada/'
)
driver.maximize_window()
pause(5)
driver.find_element(By.XPATH,"//*[@id='gz-time-slot-calendar']/ul/li[4]/a").click()
pause(10)

Solution

  • Change your XPath to this:

    data_moment = "2022-10-30"
    driver.find_element(By.XPATH,f"//*[@id='gz-time-slot-calendar']//a[@data-moment='{data_moment}']").click()