Search code examples
python-2.7seleniumselenium-webdriverwebdrivergetattribute

How to find the element part of the anchor tag


I am totally new to the selenium. Please accept apologies for asking daft or silly question.

I have below on the website. What I am interested is that how can I get the data-selectdate value using selenium + python . Once I have the data-selectdate value, I would like to compare this against the the date I am interested in.

You help is deeply appreciated.

Note: I am not using Beautiful soup or anything.

Cheers

<table role="grid" tabindex="0" summary="October 2018">
  <thead>
    <tr>
      <th role="columnheader" id="dayMonday"><abbr title="Monday">Mon</abbr></th>
      <th role="columnheader" id="dayTuesday"><abbr title="Tuesday">Tue</abbr></th>
      <th role="columnheader" id="dayWednesday"><abbr title="Wednesday">Wed</abbr></th>
      <th role="columnheader" id="dayThursday"><abbr title="Thursday">Thur</abbr></th>
      <th role="columnheader" id="dayFriday"><abbr title="Friday">Fri</abbr></th>
      <th role="columnheader" id="daySaturday"><abbr title="Saturday">Sat</abbr></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td role="gridcell" headers="dayMonday">
        <a data-selectdate="2018-10-22T00:00:00+01:00" data-selected="false" id="day22" 
          class="day-appointments-available">22</a>
      </td>
      <td role="gridcell" headers="dayTuesday">
        <a data-selectdate="2018-10-23T00:00:00+01:00" data-selected="false" id="day23" 
          class="day-appointments-available">23</a>
      </td>
      <td role="gridcell" headers="dayWednesday">
        <a data-selectdate="2018-10-24T00:00:00+01:00" data-selected="false" id="day24" 
          class="day-appointments-available">24</a>
      </td>
      <td role="gridcell" headers="dayThursday">
        <a data-selectdate="2018-10-25T00:00:00+01:00" data-selected="false" id="day25" 
          class="day-appointments-available">25</a>
      </td>
      <td role="gridcell" headers="dayFriday">
        <a data-selectdate="2018-10-26T00:00:00+01:00" data-selected="false" id="day26" 
          class="day-appointments-available">26</a>
      </td>
      <td role="gridcell" headers="daySaturday">
        <a data-selectdate="2018-10-27T00:00:00+01:00" data-selected="false" id="day27" 
          class="day-appointments-available">27</a>
      </td>
    </tr>
  </tbody>
</table>

Solution

  • To get the values of the attribute data-selectdate you can use the following solution:

    elements = driver.find_elements_by_css_selector("table[summary='October 2018'] tbody td[role='gridcell'][headers^='day']>a")
    for element in elements:
        print(element.get_attribute("data-selectdate"))