Home > Net >  How to select html tr elements with a specific td class
How to select html tr elements with a specific td class

Time:09-13

I am using selenium to scrape a website, the website has a calendar and i want to get the days out of that calendar that have available times. I have inspected the calendar and found these elements

<table >
   <thead>
      <tr>
         <th scope="col"><span title="Monday">Mån</span></th>
         <th scope="col"><span title="Tuesday">Tis</span></th>
         <th scope="col"><span title="Wednesday">Ons</span></th>
         <th scope="col"><span title="Thursday">Tor</span></th>
         <th scope="col"><span title="Friday">Fre</span></th>
         <th scope="col" ><span title="Saturday">Lör</span></th>
         <th scope="col" ><span title="Sunday">Sön</span></th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td ><span >29</span></td>
         <td ><span >30</span></td>
         <td ><span >31</span></td>
         <td ><span >1</span></td>
         <td ><span >2</span></td>
         <td ><span >3</span></td>
         <td ><span >4</span></td>
      </tr>
      <tr>
         <td ><span >5</span></td>
         <td ><span >6</span></td>
         <td ><span >7</span></td>
         <td ><span >8</span></td>
         <td ><span >9</span></td>
         <td ><span >10</span></td>
         <td ><span >11</span></td>
      </tr>
      <tr>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">12</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">13</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">14</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">15</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">16</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">17</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">18</a></td>
      </tr>
      <tr>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">19</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">20</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">21</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">22</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">23</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">24</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">25</a></td>
      </tr>
      <tr>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">26</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">27</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">28</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">29</a></td>
         <td  data-handler="selectDay" data-event="click" data-month="8" data-year="2022"><a  href="#">30</a></td>
         <td ><span >1</span></td>
         <td ><span >2</span></td>
      </tr>
   </tbody>
</table>

as you can see the days that have times available have td class= " available" those are the days I want to save and keep in a list.

I have managed to get the day number printed out so I seem to have all the rows saved as elements since when I do

avail = driver.find_elements(By.CLASS_NAME,"ui-datepicker-calendar")

for elem in avail:
    print(elem.text)

the output I get is 29,30,31,1,2,3,4...

How do I only save the rows that have td class= " available"?

CodePudding user response:

UPD
Since the parent td class may contain additional class names we can use contains instead of equals and use not to exclude unavailable dates. As following:

avail = driver.find_elements(By.XPATH,"//td[not(contains(@class,'unavailable'))]//a[@class='ui-state-default']")

for elem in avail:
    print(elem.text)
  • Related