0

Im trying to get the current active day on the calendar. when i use this:

driver.find_element_by_xpath("//a[@class='ui-state-default ui-state-highlight ui-state-active']")

it cant find the element

when i use this as the paramater it works

driver.find_element_by_xpath(".//*[@id='ui-datepicker-div']/table/tbody/tr[2]/td[6]/a")

But i believe that is static which is why i cant use it to look for the active day.

can you guys help me out ?

<!DOCTYPE html>
<html>
<head>
<body onload="if(typeof(window.parentOpeningScript)=='function'){ parentOpeningScript(); };">
<script type="text/javascript">
<div id="floatingMenuDiv" data-role="header" data-id="persistheader" data-position="fixed">
<div id="printPageHeader" class="printOnly">
<div id="primaryContainer" class="clr" style="margin-top: 100px;">
<div id="copyrightMessage" class="clr">
<ul id="ui-id-1" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none;"></ul>
<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" style="position: absolute; top: 278.767px; left: 443.133px; z-index: 1; display: block;">
<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">
<table class="ui-datepicker-calendar">
<thead>
<tbody>
<tr>
<tr>
<td class=" ui-datepicker-week-end " data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
<td class=" " data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
<td class=" " data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
<td class=" " data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
<td class=" " data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
<td class=" ui-datepicker-days-cell-over ui-datepicker-current-day ui-datepicker-today" data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
<a class="ui-state-default ui-state-highlight ui-state-active ui-state-hover" href="#">9</a>
</td>
<td class=" ui-datepicker-week-end " data-handler="selectDay" data-event="click" data-month="5" data-year="2017">
</tr>
<tr>
<tr>
<tr>
</tbody>
</table>
<div class="ui-datepicker-buttonpane ui-widget-content">
</div>
</body>
</html>
  • See [this](https://stackoverflow.com/questions/3881044/how-to-get-html-elements-with-multiple-css-classes) SO question. – Jeroen Heier Jun 15 '17 at 03:52

1 Answers1

0

Without seeing the original HTML code this is a bit of a presumptive answer, but should work:

driver.find_element_by_xpath(".//*[@id='ui-datepicker-div']/table/tbody/tr/td/a[contains(@class, 'ui-state-active')]")

This will find the first anchor element within any row within that table. You could narrow this down further still, but this should accomplish what you're looking to do.

stuartmccoll
  • 188
  • 1
  • 11
  • Thanks, unfortunately it still doesnt work. i added the Html code i hope that is enough. i thought //a[@class=...] should look for the class anywhere, at least the way i understood – chase devilla Jun 09 '17 at 10:53
  • I believe to look for the class anywhere you'd want: `driver.find_element_by_xpath("//*[@class='ui-state-active']")` I think my original answer probably hasn't worked as you've got an open `thead` element outside the `tbody` which doesn't have a closing tag. `driver.find_element_by_xpath(".//*[@id='ui-datepicker-div']/table/thead/tbody/tr/td/a[@class='ui-state-active']")` That looks like it'll line up better with your HTML, but you'd want to close that `thead` somewhere and amend accordingly. – stuartmccoll Jun 09 '17 at 10:56
  • found the reason why "ui-state-default ui-state-highlight ui-state-active ui-state-hover" i was missing ui-state-hover i dont see it when i inspect the element. – chase devilla Jun 09 '17 at 11:03
  • Ah! Good spot :). Depends what you're trying to achieve there, I suppose, but I'd think that if you're just after the active day then you wouldn't necessarily want to include the hover class when attempting to select the element. I'd only be looking to include the active class, as I'm assuming that's the main differentiator from other days in the calendar? – stuartmccoll Jun 09 '17 at 11:07
  • seems to need all 4 class. cant get it to work with just 1 – chase devilla Jun 09 '17 at 11:15
  • Think this is my lack of sleep kicking in, something like [contains(@class, ‘ui-state-active’)] rather than the current class selector would grab any element that contains that class. In this situation I’m assuming that only one element will have that class at any one time. – stuartmccoll Jun 09 '17 at 11:18
  • I've amended my original answer to include this, if you'd like to mark it as correct/answered. – stuartmccoll Jun 09 '17 at 11:53