0

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)

Available date

Line of code 23th September

<td class="day activeClass"title="Book">23</td>
  • I want to iterate all dates of the datepicker, find available dates and select one of them. Then I want to do the same thing for the appointment time. What kind of additional information do you need? I already posted the datepicker and the html above – Elouali Issam Feb 08 '23 at 11:44
  • @СергейКох any idea how to achieve this? – Elouali Issam Feb 08 '23 at 11:55
  • Anyone cares to help? – Elouali Issam Feb 08 '23 at 12:13
  • _When available, the dates are in green_: I don't see any green `` in the snapshot provided. – undetected Selenium Feb 08 '23 at 13:21
  • That's because I can't know when they are going to be available to locate them with an attribute. That's why I want I way to iterate all the dates and loop until I find the green dates @undetectedSelenium – Elouali Issam Feb 08 '23 at 13:31
  • @undetectedSelenium – Elouali Issam Feb 08 '23 at 13:33
  • How does the element looks like _When available_ ? – undetected Selenium Feb 08 '23 at 13:38
  • I will edit the post to show how does an available date look like @undetectedSelenium – Elouali Issam Feb 08 '23 at 13:39
  • Moreover, in Feb 2023 no dates are available. How would you go about searching for Mar, Apr, May, Jun, Jul, Aug, Sep? – undetected Selenium Feb 08 '23 at 13:42
  • @undetectedSelenium please check the post again. Note that the availabilities are just whiting the month . You cannot book an appointment for March while we're still on February – Elouali Issam Feb 08 '23 at 14:03
  • @undetectedSelenium – Elouali Issam Feb 08 '23 at 14:14
  • @EloualiIssam 1) The question is _Feb, 2023_ being the current month it is available as default, but do you expect even _Sep, 2022_ to be present in the table? 2) Incase it's available would you be able to open up the _Sep, 2022_ calander? – undetected Selenium Feb 08 '23 at 14:22
  • @undetectedSelenium when I couldn't find what an available date looks like in real time , I found a post in another forum just to show you how an available element looks like – Elouali Issam Feb 08 '23 at 14:24
  • @undetectedSelenium the current month is always shown by default. You cannot expect available dates in March while we are still in February. The available dates are released only within the current month. Only – Elouali Issam Feb 08 '23 at 14:28
  • @undetectedSelenium – Elouali Issam Feb 08 '23 at 14:36
  • Then how would you select a date from _Sep, 2022_ which you have shown is available? – undetected Selenium Feb 08 '23 at 14:39
  • @undetectedSelenium I said I found a post in another forum , a guy who luckily found an available date when he logged in then he posted the html of the available date along with the picture of the datepicker in green. I did that just to show you how an available element looks like – Elouali Issam Feb 08 '23 at 14:41
  • My question is different, current month being _Feb 2023_ and displayed in the UI, how would open the _Oct 2022_ calander to select a date from that month? – undetected Selenium Feb 08 '23 at 14:47
  • You click on the February 2023 above the datepicker it gives the all the months then click again on 2023 to see the years then click on year 2022 then October @undetectedSelenium how is that gonna help solving my issue?? – Elouali Issam Feb 08 '23 at 14:51
  • So where do you need help? 1) click above the datepicker 2) click on year 2022 3) click on October 4) click on available date in Oct 2022 – undetected Selenium Feb 08 '23 at 14:55
  • @undetectedSelenium Neither of those, you didn't get my point. When you click to open the calendar, the current month is visible by default. The appointments are released only within the current month. So Why would I go to a previous month or next month???? – Elouali Issam Feb 08 '23 at 14:58
  • @undetectedSelenium I didn't mean to be rude but you didn't get my point. Thank you so much for your time – Elouali Issam Feb 08 '23 at 15:01
  • [link](https://coderanch.com/t/754502/languages/Iterate-elements-attributes-selenium-python) please check this link it describes my issue @undetectedSelenium – Elouali Issam Feb 08 '23 at 15:05

1 Answers1

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352