Answer the question
In order to leave comments, you need to log in
Xpath how to get 1 first element?
In the google chrome console $x("//*[@class='name_class'][1]")
returns all elements with the given class and only the first element is needed.
In the program (c# lib: htmlagilitypack):
if (docURI.DocumentNode.SelectSingleNode("//*[@class='name_class'][1]") != null)
MessageBox.Show("docURI.DocumentNode.SelectSingleNode("("//*[@class='name_class'][1]")).InnerText;
Answer the question
In order to leave comments, you need to log in
try like this:
var elems = $x('//*[@class="name_class"]'),
firstElem;
if(elems.length) {
firstElem = elems[0];
}
console.log(firstElem);
Parenthesize the locator and take the first one:$x("(//*[@class='name_class'])[1]")
You can look here:
https://msdn.microsoft.com/ru-ru/library/ms256086.aspx
Good luck!)
Try explicitly specifying the element. For example: "//script[@class='name_class'][1]". Also, using last(), you can take the last element and subtract the number of elements in total (if you know how many of them) or use contains().
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question