使用vb.net如何获取选择下拉列表中的所有选项(webbrowser)

时间:2014-05-07 14:05:33

标签: vb.net webbrowser-control

我正在尝试动态获取网页下拉列表中的所有选项。

    <select name="ctl00$ctl00$Content$contentMain$Home1$ReportFilter$drpGroup" id="ctl00_ctl00_Content_contentMain_Home1_ReportFilter_drpGroup">
    <option selected="selected" value="-1">[select group]</option>
    <option value="0">[all groups]</option>
    <option value="2103">Alpha Phi Omega Students</option>
    <option value="2319">ART 480</option>
    <option value="2352">ENGL 111 - W14</option>
    <option value="2184">Lambda Pi Eta Group (Communication Honorary Society)</option>
    <option value="2093">ME and SW Students</option>
    <option value="2130">MGT 351 Students </option>
    <option value="2285">MGT MKT 451 </option>
    <option value="2313">NURS 101 W14</option>
    <option value="2282">Nursing 480 Students</option>
    <option value="2063">Presentation Faculty</option>
    <option value="2051">Presentation Students</option>
    <option value="2101">Roberts Fellows Students</option>
    <option value="2094">Students 101 Nursing</option>
    <option value="2320">SW 215 </option>
    <option value="2309">TE Students</option>

</select></div>

在我的代码中,我知道如果我知道值

,我可以选择每一个
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("select")
                If element.GetAttribute("name") = "ctl00$ctl00$Content$contentMain$Home1$ReportFilter$drpGroup" Then

                   element.SetAttribute("value", "2103") 'Replace 2103 with whatever you want
                End If

            Next

在设置属性

之前,如何获取所有值并将它们放入集合或数组中

1 个答案:

答案 0 :(得分:1)

我会使用正则表达式:

导入以下内容:

Imports System.Text.RegularExpressions

声明以下变量:

Dim text1 As String = ""
Dim text2 As String = ""
Dim text3 As String = "</option>"
Dim SelectId As String = "ctl00_ctl00_Content_contentMain_Home1_ReportFilter_drpGroup" 'Change to the Select element ID

请记住将SelectId变量更改为选择ID

添加以下功能:

Public Sub Process()
    Dim txt As String = text1
    Dim re1 As String = "(<OPTION)"
    Dim re2 As String = ".*?"
    Dim re3 As String = "(<\/OPTION>)"
    Dim r1 As Regex = New Regex(re1 + re2 + re3, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    Dim m1 As Match = r1.Match(txt)
    If (m1.Success) Then
        text2 = m1.ToString
        text1 = text1.Replace(m1.ToString, "")
    End If
    Dim txt2 As String = text2
    Dim re4 As String = "(>)"
    Dim re5 As String = ".*?"
    Dim re6 As String = "(<)"
    Dim r2 As Regex = New Regex(re4 + re5 + re6, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    Dim m2 As Match = r2.Match(txt2)
    If (m2.Success) Then
        Dim optionValue As String = m2.ToString.Replace(">", "")
        optionValue = optionValue.Replace("<", "")
        If optionValue = text3 Then
            Exit Sub
        Else
            text3 = optionValue
            TextBox1.Text &= optionValue & Environment.NewLine
            'Or ListBox1.Items.Add(optionValue)
            Process()
        End If
    End If
End Sub

现在假设您有一个按钮,当您单击它时将列出TextBox1中的选项值

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim txt As String = WebBrowser1.Document.Body.OuterHtml
    Dim re2 As String = "(id)"
    Dim re3 As String = "(=)"
    Dim re4 As String = "(" & SelectId & ")"
    Dim re5 As String = ".*?"
    Dim re8 As String = "(<\/SELECT>)"
    Dim r As Regex = New Regex(re2 + re3 + re4 + re5 + re8, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    Dim m As Match = r.Match(txt)
    If (m.Success) Then
        text1 = m.ToString
    End If
    Process()
End Sub

它和我一起工作!!试试让我知道。我的来源: visual basic tutorials