S
S
sivabur2015-05-11 21:17:53
HTML
sivabur, 2015-05-11 21:17:53

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

4 answer(s)
M
Mykola Dzedzinskyi, 2015-05-11
@sivabur

try like this:

var elems = $x('//*[@class="name_class"]'),
      firstElem;
if(elems.length) {
   firstElem = elems[0];
}
console.log(firstElem);

R
RayRom, 2016-01-18
@RayRom

Parenthesize the locator and take the first one:
$x("(//*[@class='name_class'])[1]")

R
Ramsay Ramsay, 2015-05-11
@Ramsays

You can look here:
https://msdn.microsoft.com/ru-ru/library/ms256086.aspx
Good luck!)

M
MrCarlione, 2015-05-12
@MrCarlione

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 question

Ask a Question

731 491 924 answers to any question