1

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>
yong
  • 13,357
  • 1
  • 16
  • 27
akumar
  • 49
  • 1
  • 9
  • can you share the link of the website as its look like current html not showing relevant element – thebadguy Sep 21 '18 at 12:47
  • Unfortunately, the URL updates the calendar dynamically. So I don't think It would help us. In addition to this, URL comes three four pages down after log in credentials. – akumar Sep 21 '18 at 13:04
  • Possible duplicate of [How to get attribute of element from Selenium?](https://stackoverflow.com/questions/30324760/how-to-get-attribute-of-element-from-selenium) – JeffC Sep 21 '18 at 15:00
  • Take a look at ^ link... you would get the `data-selectdate` attribute and it will return what you want as a string. From there you can do a string comparison to your existing date, etc. – JeffC Sep 21 '18 at 15:01

2 Answers2

2

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"))
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can use get_attribute api of element class to read attribute value of element.

css_locator = "table tr:nth-child(1) > td[headers='dayMonday'] > a"
ele = driver.find_element_by_css_selector(css_locator)
selectdate = ele.get_attribute('data-selectdate')
yong
  • 13,357
  • 1
  • 16
  • 27