I am scraping a website using selenium python to take an appointment. I click on the calendar to chose which date is available, and the available dates are on green.
Pic of the calendar:
https://i.stack.imgur.com/Fyoii.jpg[
^]
HTML code of the calendar:
<div class="datepicker-days" style="display: block;">
<table class=" table-condensed">
<thead>
<tr>
<th class="prev" style="visibility: hidden;">«</th>
<th colspan="5" class="datepicker-switch">September 2022</th>
<th class="next" style="visibility: hidden;">»</th>
</tr>
<tr>
<th class="dow">Su</th>
<th class="dow">Mo</th>
<th class="dow">Tu</th>
<th class="dow">We</th>
<th class="dow">Th</th>
<th class="dow">Fr</th>
<th class="dow">Sa</th>
</tr>
</thead>
<tbody>
<tr>
<td class="old disabled day disabled" title="Not Allowed">28</td>
<td class="old disabled day disabled" title="Not Allowed">29</td>
<td class="old disabled day disabled" title="Not Allowed">30</td>
<td class="old disabled day disabled" title="Not Allowed">31</td>
<td class="disabled day disabled" title="Not Allowed">1</td>
<td class="disabled day disabled" title="Not Allowed">2</td>
<td class="disabled day disabled" title="Not Allowed">3</td>
</tr>
<tr>
<td class="disabled day disabled" title="Not Allowed">4</td>
<td class="disabled day disabled" title="Not Allowed">5</td>
<td class="disabled day disabled" title="Not Allowed">6</td>
<td class="disabled day disabled" title="Not Allowed">7</td>
<td class="disabled day disabled" title="Not Allowed">8</td>
<td class="disabled day disabled" title="Not Allowed">9</td>
<td class="disabled day disabled" title="Not Allowed">10</td>
</tr>
<tr>
<td class="disabled day disabled" title="Not Allowed">11</td>
<td class="disabled day disabled" title="Not Allowed">12</td>
<td class="disabled day disabled" title="Not Allowed">13</td>
<td class="disabled day disabled" title="Not Allowed">14</td>
<td class="disabled day disabled" title="Not Allowed">15</td>
<td class="disabled day disabled" title="Not Allowed">16</td>
<td class="disabled day disabled" title="Not Allowed">17</td>
</tr>
<tr>
<td class="disabled day disabled" title="Not Allowed">18</td>
<td class="disabled day disabled" title="Not Allowed">19</td>
<td class="disabled day disabled" title="Not Allowed">20</td>
<td class="disabled day disabled" title="Not Allowed">21</td>
<td class="disabled day disabled" title="Not Allowed">22</td>
<td class="day activeClass" title="Book">23</td>
<td class="day disabled offday" title="Off Day">24</td>
</tr>
<tr>
<td class="day disabled offday" title="Off Day">25</td>
<td class="day activeClass" title="Book">26</td>
<td class="day activeClass" title="Book">27</td>
<td class="day activeClass" title="Book">28</td>
<td class="day activeClass" title="Book">29</td>
<td class="active day activeClass" title="Book">30</td>
<td class="new disabled day disabled" title="Not Allowed">1</td>
</tr>
<tr>
<td class="new disabled day disabled" title="Not Allowed">2</td>
<td class="new disabled day disabled" title="Not Allowed">3</td>
<td class="new disabled day disabled" title="Not Allowed">4</td>
<td class="new disabled day disabled" title="Not Allowed">5</td>
<td class="new disabled day disabled" title="Not Allowed">6</td>
<td class="new disabled day disabled" title="Not Allowed">7</td>
<td class="new disabled day disabled" title="Not Allowed">8</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="7" class="today" style="display: none;">Today</th>
</tr>
<tr>
<th colspan="7" class="clear" style="display: none;">Clear</th>
</tr>
</tfoot>
</table>
</div>
The available dates have the same attribute
title
, so I made the following Xpath to find them,
//div[@class='datepicker-days']//tbody//td[@title='Book']
It works, and I print them using a for loop:
available_dates = driver.find_element_by_xpath("//div[@class='datepicker-days']//tbody//td[@title='Book']")
for date in available_dates:
print(date.text)
Output:
23
26
27
28
29
30
When I click on a day, if there's an available time, I can continue filling the form normally, but when there is no time available during that day, it shows an error message
Slot not availble for requested date
What I want to do is to iterate through those available dates, pick a time if it's available and continue filling the form. If there is no time available and it shows an error message, I want to go to the next date and repeat the process until I find an available time.
I made this script to click on date
23, but I'm not sure if it's going to work or not because the dates are no longer available and I can't test it out.
The problem is that the available dates don't have any different attribute that I can locate them with, so I don't know how can I iterate through them. Each row is represented by
tr attribute with an index from
1 to
6. And each
tr has
7 td s, for example, the Xpath of the date
23 is
/html/body/div[6]/div[1]/table/tbody/tr[4]/td[6]
.
I had the idea of appending the available dates in a list and iterate through it, but I can't do that because I can't locate them automatically. I'm really stuck at this point and I really need help. I hope I made what I want to do obvious.
What I have tried:
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
try:
available_dates = driver.find_element(By.XPATH, "//div[@class='datepicker-days']//tbody//td[@title='Book']")
if available_dates:
for date in available_dates:
ad = driver.find_element(By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr[4]/td[6]').click()
message = WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.XPATH, '//*[@id="slot_tr"]/td[2]')))
error_message = message.text
if 'not available' in error_message:
print('date not available')
continue
else:
else:
print('No time available')
sys.exit()
except:
sys.exit()