I have HTML like that:
<tbody>
<tr>
<td >
<img src="lock.img" alt="Row locked">
</td>
<td >
</td>
<td >
</td>
<td >
</td>
</tbody>
I would like to create xpath which select for me first which is unlocked.
Only locked ID has xpath .//td/img.
I created something like that:
.//td[not(img)]/following-sibling::td[@class,'ID']
But it's not working...
I am able to find td via xpath .//td[not(img)]
, but when I add condition for ID, then I see all td, no matters if is with img
or not.
Maybe there is a simple solution which I could not find.
Please help me.
CodePudding user response:
To get all td
elements having no img
child elements you can use this XPath:
//td[not(.//img)]
BTW, your XML here is invalid.
It should be something like this:
<tbody>
<tr>
<td >
<img src="lock.img" alt="Row locked"></img>
</td>
<td >
</td>
<td >
</td>
<td >
</td>
</tr>
</tbody>
CodePudding user response:
If I understand you correctly your xml could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<tbody>
<tr>
<td >
<img src="lock.img" alt="Row locked"/>
I should NOT be found
</td>
<td >I should NOT be found</td>
<td >I should NOT be found</td>
<td >I should be found</td>
<td >I should NOT be found</td>
<td >I should be found</td>
<td >
<img src="lock.img" alt="Row locked"/>
I should NOT be found
</td>
<td >I should NOT be found</td>
<td >I should NOT be found</td>
<td >I should be found</td>
</tr>
</tbody>
.//td[not(img)]
selects also the td with @class='ID' .
To only select the td with @class='LOCK' use .//td[@class='LOCK'][not(img)]
following-sibling::td[@class,'ID']
selects all the following siblings with @class='ID'.
To get only the first add an extra [1]
to tell to only to use the first.
(It is the shorthand for [position()=1]
)
The XPath would then be:
.//td[@class='LOCK'][not(img)]/following-sibling::td[@class='ID'][1]
This will select the rows with the text 'I should be found'