Erwan Erwan - 5 months ago 7
Java Question

Syntax error on selenium xpath expression

I'm building a selenium

findElement by.xpath
expression, very complex, and it appears there is a syntax error.

I don't really know XML language so I made mistake(s) obviously, could someone tell where are the mistake(s) ? :)

Thanks!

I have a list of projects on a web page, in each project there are sub-projects, I want a specific one, I know the
name
and the
subName
.

The html code is something like :

<table id="list_proj" class="table tablebas table-striped table-bordered">
<thead>
<tbody style="height:1em;overflow-y:scroll"></tbody>
<tbody style="height:1em;overflow-y:scroll"></tbody>
<tbody style="height:1em;overflow-y:scroll">
<tr class="caption">
<td class="app_name" style="background-color:#EFEFEF;" colspan="11">
<b>
<a href="?page=applications&mode=edit&app_id=3" style="padding-left:10px;" data-icon=""> Application SEVo</a>
</b>
</td>
</tr>
<tr class="data"></tr>
<tr class="data"></tr>
<tr class="data">
<td title="Pas de commentaire !" style="text-align:left">
<img alt="MCO" src="/colibri/images/corner-dots.gif"></img>
<a href="?page=projects&mode=view&project_id=9" style="padding-left:5px">MCO</a>
</td>
<td>0.50</td>
<td></td>
<td colspan="2">
</tr>
<tr class="data">
</tbody>
<tbody style="height:1em;overflow-y:scroll"></tbody>
</table>


And my xpath expression :

driver.findElement(
By.xpath(
"((//a[contains(text(),'"+name+"')])[1]
.(ancestor::td[@class='caption'])[1]
.following-sibling::td[@class='data']
.descendant::(a[contains(text(),'"+subName+"')])[1])[1]"
));


Sorry for the mess ^^'

Answer

It should be something like:

//a[contains(text(),'"+name+"')]/ancestor::tr[@class='caption']/following-sibling::tr[@class='data']//a[contains(text(),'"+subName+"')]

Don't use index [1] everytime, If you are sure that only one element would be returned, there is no point of putting index in every descendant or ancestor call.

Comments