selecting the last node from a list of matches
I'm trying to figure out a way of finding the last node that matches a given xpath using last() function. The problem is that the last element of path also has a constraint specified.
"//div[@id='someId']/ul/li/div[@class='class1 class2 ']/span[@class='someType2 ']"
if I use
"//d开发者_StackOverflowiv[@id='someId']/ul/li/div[@class='class1 class2 ']/span[@class='someType2 ']' and last()]"
it still matches multiple nodes. Maybe one of the reasons is that the last div tag in the path contains 2 span elements. please help me select the last node which matches the above path.
Thanks and Regards,
VamyipIf your xml is xhtml, why don't use CSS selectors ? If I'm not mistaken, the selectors should be
#someId > ul > li > div.class1.class2 > span.someType2
#someId > ul > li > div.class1.class2 > span.someType2:last
I was using xpath on html pages too, but when CSS selectors became widespread I found that they are more supported across browsers than xpath.
Use:
(//div[@id='someId']/ul/li/div[@class='class1 class2 ']
/span[@class='someType2 '])
[last()]
Do note: the brackets surrounding the expression starting with //
. This is a FAQ. []
binds stronger than //
and this is why brackets are necessary to indicate different precedence.
In selenium, you can also use javascript to retrieve elements. How about something like this?
dom=var list1 =
document.getElementById('someId').
getElementsByTagName('li');
var finallist = new Array();
for (var i=0; i<list1.length; i++) {
var list2 = list1[i].getElementsByClassName("class1 class2");
for (var j=0; j<list2.length; j++) {
var list3 = list2[j].getElementsByClassName("someType2");
for (var k=0; k<list3.length; k++) {
finallist.push(list3[k];
}
}
}
finallist.pop()
http://seleniumhq.org/docs/04_selenese_commands.html#locating-by-dom
精彩评论