I am trying to code a script to book appointments inside this datepicker.
When available, the dates are in green. The problem is I don't know when they can be available to locate them directly with an attribute ( class or title...)
Selecting an available date triggers a drop-down list for appointment time and another one for visa type( hidden elements)
How can I :
1/Iterate all dates, loop until available ones are found, select one of them?
2/Iterate appointment times ,loop until available ones are found and select one of them?
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import sys
available_dates = driver.find_elements(By.XPATH, "//div[@class='datepicker-days']/table/tbody/tr/td[not(contains(@class, 'disabled'))]")
for date in available_dates:
print(date.text)
Line of code 23th September
<td class="day activeClass"title="Book">23</td>
To extract the available booking days you have to induce WebDriverWait for visibility_of_all_elements_located() and using List Comprehension you can use either of the following locator strategies:
Using CSS_SELECTOR and text attribute:
driver.get('https://example.com')
try:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.datepicker-days > table.table-condensed tr td.day.activeClass[title='Book']")))])
except TimeoutException:
print("No booking dates available for the current month")
driver.quit()
Using XPATH and get_attribute("innerHTML")
:
driver.get('https://example.com')
try:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='datepicker-days']/table[@class=' table-condensed']//tr//td[@class='day activeClass' and @title='Book']")))])
except TimeoutException:
print("No booking dates available for the current month")
driver.quit()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException