Question

I am trying to dynamically get all the options in a dropdown on a webpage.

    <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>

In my code I know I can select each one if I knew the value

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

How do I grab all the values and put them in a collection or array before setting the attribute

Was it helpful?

Solution

I would do it using regex:

Import the following:

Imports System.Text.RegularExpressions

Declare the following variables:

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

Remember to change the SelectId variable to the select ID

Add the following function:

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

Now lets say you have a button that when you click it will list the options value in 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

It worked with me!! Try and let me know. My source: visual basic tutorials

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top