Reputation: 1951
I am using the following code to access a link (for phpunit/selenium):
//td[normalize-space() ='Test title 2']/following-sibling::td[3]/a[.='delete']
using an XPath checker in FireFox it returns 7 elements (because there are 7 links matching "test title 2"), but when I add [1]
at the end:
//td[normalize-space() ='Test title 2']/following-sibling::td[3]/a[.='delete'][1]
it still returns 7 links. What am I doing wrong here?
Upvotes: 2
Views: 3788
Reputation: 1988
When you add [1]
in the end of your expression, you select the first a
child of each ...td[3]
(i.e. 7 a
child nodes). You can change your query to:
xpath=(//td[normalize-space() ='Test title 2']/following-sibling::td[3]/a[.='delete'])[1]
or in case you use webdriver (xpath prefix is not needed):
(//td[normalize-space() ='Test title 2']/following-sibling::td[3]/a[.='delete'])[1]
This will select the first a
from the entire set of a
children of ...td[3]
elements.
Refer to XPath Examples for more tutorials.
Upvotes: 6
Reputation: 38444
As the spec says:
The location path
//para[1]
does not mean the same as the location path/descendant::para[1]
. The latter selects the first descendantpara
element; the former selects all descendantpara
elements that are the firstpara
children of their parents.
Therefore,
//td[normalize-space() ='Test title 2']/following-sibling::td[3]/descendant::a[.='delete'][1]
will do a better job in your case.
Upvotes: 0