Home > Back-end >  How to find the respective element using xpath contains() function -selenium webdriver
How to find the respective element using xpath contains() function -selenium webdriver

Time:09-17

This is the html snippet

<div class="ext-cal-wk-ct">
   <table>
       <tbody>
           <tr>
               <td><div>13</div></td>
               <td><div>14</div></td>
               <td><div>15</div></td>
           <tr>
           <tr>
               <td><div>Calender note WebElement[no 13]</div></td>
               <td><div>Calender note WebElement[no 14]</div></td>
               <td><div>Calender note WebElement[no 15]</div></td>
           </tr>
       </tbody>
   </table>
</div>

I want to find the respective element using xpath by the element no for example: if I use contains(text(),'13')--> WebElement no 13 should be returned for contains(text(),'14')--> WebElement no 14 should be returned and so on

I have used two xpaths to find the elements individually but don't know how to connect them

//div[@class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[contains(text(),'13')]

//div[@class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[contains(text(),'Tim Cook')]

need some help on this

I want a xpath where I will pass the date and it will return me the calender note for that particular date

CodePudding user response:

basically, contains is for partial matching.

When you write

contains(text(),'13')

there are 2 matching nodes,

<div>13</div>

and

<div>WebElement no 13</div>

so I would suggest to use :

//div[@class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[text(),'13']

to locate

<div>13</div>

or

//div[@class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[text()='WebElement no 13']

to locate this :

<div>WebElement no 13</div>

CodePudding user response:

You can use below syntax by using contains in xpath

//tagname[contains(text(),'textname')]

I guess in your case you should consider exact match :

<td><div>15</div></td> : for this element

xpath => //td/div[text()='13']

and for this element : <td><div>WebElement no 13</div></td>

xpath => //td/div[text()='WebElement no 13']

i hope there is unique text in dom for given html snippet.

  • Related