Need help creating XPATH selectors which depends on the name of the element

I wonder if someone can help me. I have an application where user single click on folder icons to open/close it (Similar to how we navigate/explore files in windows explorer tree) - When I am recording my test in selenium IDE here is code that's generated:

<tr>
    <td>click</td>
    <td>//ul[@id='analysisTree']/li[3]/p</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>//ul[@id='analysisTree']/li[4]/p</td>
    <td></td>
</tr>

Here is a snippet of source code from the application corresponding to the collapsed state of the folders.

<li class="folder collapsed" data-nodecode="RentalValues">
<p>Rental Values</p>
<ul>
</li>
<li class="folder collapsed" data-nodecode="Affordability">
<p>Affordability</p>
<ul>
</li>

The problem with the current default element xpath locator is that if few months down the line we introduce another folder on the tree it may shift the folder position up or down the tree, which would result in wrong folder being opened. Therefore can someone please help my put together an XPath which would be more robust and would not depend on the exact location of folder in the tree but rather the name of the folder which is unique?

Cheers

Shan

Answers


You could base the selection on the data-nodecode attribute:

<tr> 
  <td>click</td> 
  <td>//ul[@id='analysisTree']/li[@data-nodecode='RentalValues']/p</td> 
  <td></td> 
</tr> 
. . . 

or on the name of the folder:

<tr> 
  <td>click</td> 
  <td>//ul[@id='analysisTree']/li[normalize-space(.)='Rental Values']/p</td> 
  <td></td> 
</tr> 
. . . 

normalize-space(.) gets the complete text contained inside the current element (li in this case) with extra whitespace stripped off.


Need Your Help

Cannot split textarea on newline created from html entities

javascript html textarea newline

I have a textarea that needs a default value including new lines so I used the technique employing HTML entities found from this answer

How can I distribute portlet pages to different environments automatically?

liferay liferay-6 portlet

I'm a newbie developing a liferay 6.1 portlet. I want a static url and static content in the page where my portlet will be. What methods are recommended and available for distributing the portlet and