私のアプリはhtmlagilityパックを使用しています。現時点では、すべての入力要素をフォームで取得できます。問題は、すべての入力要素をIDで取得していることです。私は、各入力要素の前に正確な内部のテキストラベルを含むIDによるフォームの入力要素を私に与えるように絞り込んでいます。
例:
<label for="email">Email Address:</label>
<input type="text" class="textbox" name="email" id="email" maxlength="50" value="" dir="ltr" tabindex="1"
私は "電子メールアドレス"の内部のテキストで進行ラベルを持つ入力を取得しようとしています
私はこれをどう言いますか?
ここではすべての入力要素をIDで取得するアプリケーションです。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim doc As HtmlDocument
Dim web As New HtmlWeb
doc = web.Load("http://shaggybevo.com/board/register.php")
Dim docNode As HtmlNode = doc.DocumentNode
Dim nodes As HtmlNodeCollection = docNode.SelectNodes("//input")
'SelectNodes takes a XPath expression
For Each node As HtmlNode In nodes
'Get all input elements by id
Dim id As String = node.GetAttributeValue("value", "id")
'print all input elements by id to form2 richtextbox
Form2.RichTextBox1.Text = Form2.RichTextBox1.Text & Environment.NewLine & id.ToString & name.ToString()
Form2.Show()
Next
End Sub
ありがとうございました....私はしばらくの間、VB.NETを勉強してきたと言わなければなりません。そして、このフォーラムはすばらしいされています...私はそれを見つけてうれしい..
ここでの基本的なコンセプトは、 for
属性が関連するinput
IDと一致するラベルを取得することです。
したがって、ラベルを最初に循環させ、 for
値でキーを付けた辞書にラベルのテキストを記録し、次にinputs
を循環させ、 inputs
のIDが辞書にあれば、辞書から値を取り出しますこれはラベルテキストです)を表示してください。
データをより効率的に収集する方法も変更しました(ほとんどの場合、文字列を連結するときは、stringbuilderを使用する必要があります)。
ここに書かれたコードです:
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
Form2.RichTextBox1.Text = sbText.ToString
Form2.Show()