Bart Warrot
Bart Warrot

Reputation: 1

Need a dynamic python selenium way of picking an element by xpath

This is the HTML it needs to pick from:

<tbody class="datepickerDays">
    <tr>
        <th class="datepickerWeek"><a href="#"><span>40</span></a></th>
        <td class="datepickerNotInMonth"><a href="#"><span>28</span></a></td>
        <td class="datepickerNotInMonth"><a href="#"><span>29</span></a></td>
        <td class="datepickerNotInMonth"><a href="#"><span>30</span></a></td>
        <td class=""><a href="#"><span>1</span></a></td>
        <td class=""><a href="#"><span>2</span></a></td>
        <td class="datepickerSaturday"><a href="#"><span>3</span></a></td>
        <td class="datepickerSunday"><a href="#"><span>4</span></a></td>
    </tr>
    <tr>
        <th class="datepickerWeek"><a href="#"><span>41</span></a></th>
        <td class=""><a href="#"><span>5</span></a></td>
        <td class=""><a href="#"><span>6</span></a></td>
        <td class=""><a href="#"><span>7</span></a></td>
        <td class="datepickerSelected"><a href="#"><span>8</span></a></td>
        <td class=""><a href="#"><span>9</span></a></td>
        <td class="datepickerSaturday"><a href="#"><span>10</span></a></td>
        <td class="datepickerSunday"><a href="#"><span>11</span></a></td>
    </tr>
    <tr>
        <th class="datepickerWeek"><a href="#"><span>42</span></a></th>
        <td class=""><a href="#"><span>12</span></a></td>
        <td class=""><a href="#"><span>13</span></a></td>
        <td class=""><a href="#"><span>14</span></a></td>
        <td class=""><a href="#"><span>15</span></a></td>
        <td class=""><a href="#"><span>16</span></a></td>
        <td class="datepickerSaturday"><a href="#"><span>17</span></a></td>
        <td class="datepickerSunday"><a href="#"><span>18</span></a></td>
    </tr>
    <tr>
        <th class="datepickerWeek"><a href="#"><span>43</span></a></th>
        <td class=""><a href="#"><span>19</span></a></td>
        <td class=""><a href="#"><span>20</span></a></td>
        <td class=""><a href="#"><span>21</span></a></td>
        <td class=""><a href="#"><span>22</span></a></td>
        <td class=""><a href="#"><span>23</span></a></td>
        <td class="datepickerSaturday"><a href="#"><span>24</span></a></td>
        <td class="datepickerSunday"><a href="#"><span>25</span></a></td>
    </tr>
    <tr>
        <th class="datepickerWeek"><a href="#"><span>44</span></a></th>
        <td class=""><a href="#"><span>26</span></a></td>
        <td class=""><a href="#"><span>27</span></a></td>
        <td class=""><a href="#"><span>28</span></a></td>
        <td class=""><a href="#"><span>29</span></a></td>
        <td class=""><a href="#"><span>30</span></a></td>
        <td class="datepickerSaturday"><a href="#"><span>31</span></a></td>
        <td class="datepickerNotInMonth datepickerSunday"><a href="#"><span>1</span></a></td>
    </tr>
    <tr>
        <th class="datepickerWeek"><a href="#"><span>45</span></a></th>
        <td class="datepickerNotInMonth"><a href="#"><span>2</span></a></td>
        <td class="datepickerNotInMonth"><a href="#"><span>3</span></a></td>
        <td class="datepickerNotInMonth"><a href="#"><span>4</span></a></td>
        <td class="datepickerNotInMonth"><a href="#"><span>5</span></a></td>
        <td class="datepickerNotInMonth"><a href="#"><span>6</span></a></td>
        <td class="datepickerNotInMonth datepickerSaturday"><a href="#"><span>7</span></a></td>
        <td class="datepickerNotInMonth datepickerSunday"><a href="#"><span>8</span></a></td>
    </tr>
</tbody>

The code should determine what date it is today and click on that day. I think that there is no need for month/year because the only view the program will see is the current month anyway. If your solution can provide a month-picker also, it would be great.

So we need the current date (for example: 8th, while the previous date was 5), the current day name, and the program needs to pick according to that.

Current efforts:

driver.find_element_by_xpath('//td[@class="datepickerSelected"]/a[text()="8"]').click()

But Selenium doesn't click on it.

I can't show you the entire code, or the website we are using it on because it is inside a login environment.

Upvotes: 0

Views: 37

Answers (2)

JeffC
JeffC

Reputation: 25610

To get today's date, you can use datetime. See the docs for more info. Once you have it, you can insert the day into the locator and click the element.

There are a couple problems with your locator vs the HTML that you posted.

//td[@class="datepickerSelected"]/a[text()="8"]
  1. This is looking for a TD that has a class "datepickerSelected" but it doesn't exist in the HTML you posted. I'm assuming that class only appears after you've selected a date but when you first enter the page, this won't be true so we can't use that class to locate the day we want.

  2. The text() method finds text inside of the element specified, in this case an A tag. If you look at the HTML, the text is actually inside the SPAN child of the A tag. There are a couple ways to deal with this. You can change that part of the locator to be /a/span[text()="8"] or use . which "flattens" the text from all child elements, e.g. /a[.="8"]. Either way will work.

Another problem you will have to deal with is if the day is late or early in the month, then it shows up twice in the HTML, e.g. 2 or 28. To get the right one, you need to specify the day in the SPAN under a TD with an empty class. The wrong ones have a TD with the class datepickerNotInMonth.

Taking all this into account, here's the code I would use.

import datetime

today = datetime.datetime.now().day
driver.find_element_by_xpath(f'//td[@class=""]/a[.="{today}"]').click()

The locator finds a TD that contains an empty class that has a child A that contains (the flattened) text corresponding to today's day.

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Use the following xpath to find the element.

driver.find_element_by_xpath('//td[@class="datepickerSelected"]/a[./span[text()="8"]]').click() 

Upvotes: 1

Related Questions