Home > Back-end >  How to click on the highlighted today's day within the calendar with Selenium and Python
How to click on the highlighted today's day within the calendar with Selenium and Python

Time:08-18

I'm trying to do an automation with selenium so that, for example, today it selects today's date, tomorrow selects tomorrow's date, without having to keep changing it, as it is a task completion that must occur every day, I would like to know how to select the day according to the current day automatically in the calendar, follows the page's HTML code enter image description here

<table>
  <thead>
    <tr>
      <th>D</th>
      <th>S</th>
      <th>T</th>
      <th>Q</th>
      <th>Q</th>
      <th>S</th>
      <th>S</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td><button type="button" ><div >1</div></button></td>
      <td><button type="button" ><div >2</div></button></td>
      <td><button type="button" ><div >3</div></button></td>
      <td><button type="button" ><div >4</div></button></td>
      <td><button type="button" ><div >5</div></button></td>
      <td><button type="button" ><div >6</div></button></td>
    </tr>
    <tr>
      <td><button type="button" ><div >7</div></button></td>
      <td><button type="button" ><div >8</div></button></td>
      <td><button type="button" ><div >9</div></button></td>
      <td><button type="button" ><div >10</div></button></td>
      <td><button type="button" ><div >11</div></button></td>
      <td><button type="button" ><div >12</div></button></td>
      <td><button type="button" ><div >13</div></button></td>
    </tr>
    <tr>
      <td><button type="button" ><div >14</div></button></td>
      <td><button type="button" ><div >15</div></button></td>
      <td><button type="button" ><div >16</div></button></td>
      <td><button type="button" ><div >17</div></button></td>
      <td><button type="button" ><div >18</div></button></td>
      <td><button type="button" ><div >19</div></button></td>
      <td><button type="button" ><div >20</div></button></td>
    </tr>
    <tr>
      <td><button type="button" ><div >21</div></button></td>
      <td><button type="button" ><div >22</div></button></td>
      <td><button type="button" ><div >23</div></button></td>
      <td><button type="button" ><div >24</div></button></td>
      <td><button type="button" ><div >25</div></button></td>
      <td><button type="button" ><div >26</div></button></td>
      <td><button type="button" ><div >27</div></button></td>
    </tr>
    <tr>
      <td><button type="button" ><div >28</div></button></td>
      <td><button type="button" ><div >29</div></button></td>
      <td><button type="button" ><div >30</div></button></td>
      <td><button type="button" ><div >31</div></button></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

CodePudding user response:

If you observe the HTML closely today's date i.e. 16 is highlighted as follows:

<td>
    <button type="button" >
        <div >16</div>
    </button>
</td>

Solution

To click on the highlighted day you can use either of the following locator strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "td > button.v-date-picker-table__current > div.v-btn__content").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//td/button[contains(@class, 'v-date-picker-table__current')]/div[@class='v-btn__content']").click()
    
  • Related