Question

I honestly don't know how to do this, and I am very stuck. My code is probably not worth sharing, but I will try my best to ask the question.

<div class="pricing-table">
    <div class="span4">
        <div class="plan">
            <h3>PPTP <span>$0</span></h3>
            <ul>
                <li><b>IP:</b> " (hidden)"</li>
                <li><b>Username:</b> " Guest"</li>
                <li><b>Password:</b> " (hidden)"</li>
                <li><b>Unlimited</b> " Bandwidth"</li>
                <li><b>Torrents</b> " Allowed"</li>
                <li><b>No</b> " Logging"</li>
            </ul>
        </div>
    </div>
</div>

(This is the second one)

<div class="span4">
    <div class="plan most-popular">
        <h3>Open<span>$0</span></h3>
        <div class="plan-ribbon-wrapper">
            <div class="plan-ribbon">
                Popular
            </div>
        </div>
        <ul>
            <li><b>Username:</b> user</li>
            <li><b>Password:</b> (hidden)</li>
            <li><b>TCP</b> 80, 443</li>
            <li><b>UDP</b> 53, 40000</li>
            <li><b>Unlimited</b> Bandwidth</li>
            <li><b>Torrents</b> Allowed</li>
            <li><b>No</b> Logging</li>
        </ul><a class="btn btn-large btn-primary" href=
        "%20(hidden)">Download</a>
    </div>
</div>
<ul>
    <li><b>Username:</b> user</li>
    <li><b>Password:</b> (hidden)</li>
    <li><b>TCP</b> 80, 443</li>
    <li><b>UDP</b> 53, 40000</li>
    <li><b>Unlimited</b> Bandwidth</li>
    <li><b>Torrents</b> Allowed</li>
    <li><b>No</b> Logging</li>
</ul>

Using this HTML code, I want to copy the username and password and put it into a label on my form.

Here is my code

Imports HtmlAgilityPack
Public Class Form1
Dim Prev_Pass, Prev_User, Currt_Pass, Currt_User, Info
Dim counter As Integer
Dim web As New HtmlWeb()
Dim htmldoc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
Dim htmlnodes As HtmlAgilityPack.HtmlNodeCollection = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    htmldoc = web.Load("website")
    htmlnodes = htmldoc.DocumentNode.SelectNodes("//div[@class=""pricing-table""]")
    For Each node As HtmlAgilityPack.HtmlNode In htmlnodes
        Dim releaseNode = node.SelectSingleNode("//div[@class=""plan most-popular""]")
            Info = releaseNode.SelectSingleNode("//ul/li/b[contains(text(), 'Username: ')]").InnerText
    Next
    Label3.Text = Info
End Sub
End Class
Was it helpful?

Solution

Consider the following annotations:

<li>
    <b>Username:</b> <!-- Use XPath to obtain this element. -->
    " user" <!-- This is the next sibling. -->
</li>

With these annotations in mind:

You can obtain the <b> element using XPath as you have done already and then access the username or password text by means of the NextSibling property like so:

Dim username = 
    htmldoc.DocumentNode.SelectSingleNode(
        "//ul/li/b[text()='Username:']").NextSibling.InnerHtml

Dim password =
    htmldoc.DocumentNode.SelectSingleNode(
        "//ul/li/b[text()='Password:']").NextSibling.InnerHtml

If you want to access the second occurrence of the element instead of the first, then you you should either use a more specific XPath or use SelectNodes and obtain the element located at the second index by means of an indexer like this:

Dim username =
    htmldoc.DocumentNode.SelectNodes(
        "//ul/li/b[text()='Username:']")(1).NextSibling.InnerHtml
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top