I currently this code on my app that works great to loop through a page and grab the label of an input value of a form and then print the value to a rich text box.
I am now trying to revamp the code to give me the ID the value if the label contains a specific name.
For instance if the label before the input field contains the word Username then I want the application to output the ID of the input field rather than just everything that has a label.
Here is my current code:
Dim web As HtmlAgilityPack.HtmlWeb = New HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load("http://shaggybevo.com/board/register.php")
Dim nodes As HtmlNodeCollection
' Keeps track of the labels by the associated control id
Dim labelText As New System.Collections.Generic.Dictionary(Of String, String)
' First, get the labels
nodes = doc.DocumentNode.SelectNodes("//label")
If nodes IsNot Nothing Then
For Each node In nodes
If node.Attributes.Contains("for") Then
Dim sFor As String
' Extract the for value
sFor = node.Attributes("for").Value
' If it does not exist in our dictionary, add it
If Not labelText.ContainsKey(sFor) Then
labelText.Add(sFor, node.InnerText)
End If
End If
Next
End If
nodes = doc.DocumentNode.SelectNodes("//input")
Dim sbText As New System.Text.StringBuilder(500)
If nodes IsNot Nothing Then
For Each node In nodes
' See if this input is associated with a label
If labelText.ContainsKey(node.Id) Then
' If it is, add it to our collected information
sbText.Append("Label = ").Append(labelText(node.Id))
sbText.Append(", Id = ").Append(node.Id)
sbText.AppendLine()
End If
Next
End If
RichTextBox1.Text = sbText.ToString
I think you just want to change the if statement in the last loop to: