DFSFOT February 2016

Why does my XPath get stuck on first result when I add /text()?

Here's a simulation of the HTML I am trying to use my XPath on:

<div class="stream-links">
    <div>
        <a href="link">value I need</a>
    </div>
    <div>
        <a href="link">value I need</a>
    </div>
    <div>
        <a href="link">value I need</a>
    </div>
</div>

Now, when I use the XPath pattern //div[@class='stream-links']/div/a in my browser it selects the <a ...> node. Everytime I press enter it selects the next one, but when I use the pattern //div[@class='stream-links']/div/a/text() it gets stuck on the text of the first <a ...> node so when I press enter it does not move to the next. (Using Firebug plugin on FireFox btw to inspect element)

I'm coding a program in C# and the amount of divs under the parent div is a variable so I can't use //div[@class='stream-links']/div[number here]/a/text() because I need to get all of them.

My code for using the Xpath is HtmlNodeCollection NODECOL1 = MEDOC.DocumentNode.SelectNodes("//div[@class='stream-links']/div/a[1]");

So my questions are:
1) Is there a particular reason Firebug doesn't jump to the next <a...> or is it a 'bug' on the plugin's side?
2) Will my code work nevertheless or do I need to approach it in another way?

There're a few things not right with the rest of my code so I can't see if that part of my code actually works or not, wouldn't ask question 2 if I could test it myself right now.

Answers


kjhughes February 2016

For your HTML, this XPath selects three a elements:

//div[@class='stream-links']/div/a

This XPath selects three text nodes:

//div[@class='stream-links']/div/a/text()

This XPath selects one a element:

//div[@class='stream-links']/div/a[1]

My code for using the Xpath is HtmlNodeCollection NODECOL1 = MEDOC.DocumentNode.SelectNodes("//div[@class='stream-links']/div/a[1]");

1) Is there a particular reason Firebug doesn't jump to the next or is it a 'bug' on the plugins side?

//div[@class='stream-links']/div/a[1] only selects one a element.

2) Will my code work nevertheless or do I need to approach it in another way?

There's a few things not right with the rest of my code so I can't see if that part of my code actually works or not, wouldn't ask question 2 if I could test it myself right now.

That's not a reasonable question to ask given what you've shown us. Perhaps knowing what the above XPaths return will help you answer it for yourself.

Post Status

Asked in February 2016
Viewed 2,320 times
Voted 4
Answered 1 times

Search




Leave an answer