3

I am using union to xpath and created one for day and time. Now, I am able to catch this path but when I used to click on that element using xpath, it's not getting proper time slot selected.

My xpath is as follows: .//*[@id='calendar']//*[@class='fc-first fc-last']//*[contains(@class,'fc-wed fc-col3 ui-widget-content')]|.//*[@id='calendar']//*[@class='fc-agenda-slots']//*[contains(@class,'fc-slot3')]

and need to use this xpath in for loop such way i can search each date and time slot till end of the calendar.

<table class="fc-agenda-days fc-border-separate" cellspacing="0" style="width:100%">
<thead>
  <tbody>
    <tr class="fc-first fc-last">
        <th class="fc-agenda-axis ui-widget-header fc-first"/>
        <td class="fc-sun fc-col0 fc-day-past">
        <td class="fc-mon fc-col1 ui-widget-content ui-state-highlight fc-today">
        <td class="fc-tue fc-col2 fc-day-past">
        <td class="fc-wed fc-col3 ui-widget-content">
        <td class="fc-thu fc-col4 fc-day-past">
        <td class="fc-fri fc-col5 ui-widget-content">
        <td class="fc-sat fc-col6 fc-day-past">
        <td class="fc-agenda-gutter ui-widget-content fc-last" style="display: table-cell; width: 17px;"/>
    </tr>
  </tbody>
</table>
<div style="position: absolute; z-index: 2; left: 0px; width: 100%; top: 25px;">
<div style="position: absolute; width: 100%; overflow-x: hidden; overflow-y: auto; height: 189px;">
<div style="position:relative;width:100%;overflow:hidden">
<div style="position:absolute;z-index:8;top:0;left:0">
<table class="fc-agenda-slots" cellspacing="0" style="width:100%">
    <tbody>
        <tr class="fc-slot0 ">
        <tr class="fc-slot1 fc-minor">
        <tr class="fc-slot2 fc-minor">
        <tr class="fc-slot3 fc-minor">
        <tr class="fc-slot4 fc-minor">
        <tr class="fc-slot5 fc-minor">
        <tr class="fc-slot6 ">
        <tr class="fc-slot7 fc-minor">
        <tr class="fc-slot8 fc-minor">
        <tr class="fc-slot9 fc-minor">
        <tr class="fc-slot10 fc-minor">
        <tr class="fc-slot11 fc-minor">
        <tr class="fc-slot12 ">
        <tr class="fc-slot13 fc-minor">
        <tr class="fc-slot14 fc-minor">
        <tr class="fc-slot15 fc-minor">
        <tr class="fc-slot16 fc-minor">
        <tr class="fc-slot17 fc-minor">
    </tbody>
</table>

above is my calendar code. This is my calendar image enter image description here

1
  • 3
    Yet another question when Xpath is too fragile to be usefull - seems we get few every week, yet people insist on using it (I avoid it if i can). And looking at the Xpath expression, i an NOT surprised it does not work consistently. OP: Try locating element by id, name or CSS. – Peter M. - stands for Monica Apr 11 '16 at 15:10
2

You want to accomplish your goal by having a very long xpath,

  1. Xpath expression combines two xpath with a logical operator, make it hard to maintain
  2. Xpath is less stable than css selector, make it hard to maintain

Please consider the following suggestions:

using xpath:

listOfWeekdays = driver.find_elements_by_xpath("//tr[@class='fc-first fc-last']/td")
  • you can address Sunday ~ Sat by indexing listOfWeekdays, please bear in mind the very last element(listOfWeekdays[7]) in listOfWeekdays is NOT a week day; it is element td[@class='fc-agenda-gutter ui-widget-content fc-last']

listOfWorkingHours = driver.find_elements_by_xpath("//tr[@class='fc-agenda-slots']//tr")

  • you can address working hours 0~17 by indexing listOfWorkingHours.

Using Css Selector:

listOfWeekdays = driver.find_elements_by_css_selector("tr[class='fc-first fc-last']>td")

  • you can address Sunday ~ Sat by indexing listOfWeekdays, please bear in mind the very last element(listOfWeekdays[7]) in listOfWeekdays is NOT a week day; it is element td[class='fc-agenda-gutter ui-widget-content fc-last']

listOfWorkingHours = driver.find_elements_by_css_selector("tr[@class='fc-agenda-slots']>tbody>tr")

  • you can address working hours 0~17 by indexing listOfWorkingHours.

You may ask why xpath is less desirable than css selector?

  • css selector is about identifying a web element by its attributes
  • xpath is about identifying a web element by its attributes AND its whereabouts in HTML hierarchy tree. For a web element, its whereabouts in HTML hierarchy is prone to changes where attributes tend to stay the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.