-1

Can anyone please help me how to use 'contains' in my xpath while selecting a date from datepicker. This is my xpath :

//*[@id="UserDetails.EffectiveDate_DDD_C_mt"]/tbody/tr/td[contains(text(), '2')] 

This xpath is giving me '16 elements matching'. It's giving me all the dates having digit 2. I wanted to select a specific date. I expected the xpath to give me one specific matching node.

Update copied from OP's answer attempt

Here is the first <td> of each row is different.

//**First <td> of first row----**
<td class="dxeCalendarWeekNumber_Office2010Silver" id="UserDetails.EffectiveDate_DDD_C_AUX_0_0_8">36</td> 

//First <td> of second row -------
<td class="dxeCalendarWeekNumber_Office2010Silver" id="UserDetails.EffectiveDate_DDD_C_AUX_0_0_9">37</td>

id is different which will make the date '2' as unique I tried with---

//*[@id="UserDetails.EffectiveDate_DDD_C_mt"]/tbody/tr/td[contains(text(), '2')] //td[@id='UserDetails.EffectiveDate_DDD_C_AUX_0_0_8'] 

As a result showing "0 element matching".

  • 1
    @OP - Please clean this question up. It is very confusing what you are asking and will likely end up getting closed. – Brian Sep 11 at 17:02
1

Your xpath is doing exactly what it is supposed to do: return every <td> element descended from an element with id="UserDetails.EffectiveDate_DDD_C_mt"

If you wish to select only the 2nd of the current month you can use one of these methods:

  • td[text() = '2'] - this will work if there is no white space in the element text and the element for the 2nd is not listed as '02'. If your calendar widget shows the first few days of the next month or is a floating calendar showing a certain number of days before and/or after the current date you will get more than one result.
  • If your calendar widget has more than one <td> where text() = '2' you'll need to use another method. Exactly what you use will depend on how the widget is configured.
    • If the widget uses a different style or class for the current month, you can add a filter on that.
    • If there's an attribute on the <td> that identifies the month you can add a filter on that.
    • If there's any kind of onclick attribute that's unique to the date you want, you can filter on that.

Without seeing the HTML of the widget there really isn't much more help anyone can give you.

0

Thanks Kate, i tried as you suggested the widget is configured. Here is the first of each row is different.First of first row---- 36 , First of second row -------37. id is different which will make the date '2' as unique. I tried with--- //*[@id="UserDetails.EffectiveDate_DDD_C_mt"]/tbody/tr/td[contains(text(), '2')] //td[@id='UserDetails.EffectiveDate_DDD_C_AUX_0_0_8']. As a result shwoing "0 element matching".

  • 3
    Please don't post updates as answers. You can edit your question with the information you want to add. I have edited your question for you this time, and formatted the update so it is readable. I strongly recommend you read the Help Center (sqa.stackexchange.com/help), especially the pages on asking good questions and how to answer. I also recommend you review Xpath referencing because the correct way to use two conditions is [condition1 AND condition2] – Kate Paulk Sep 12 at 16:01

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.