none
Get All Combobox Items from WebBrowser Into ComboBox RRS feed

  • Question

  • Hi ..

    I'm looking for a way to get all the DropDown Menu Items from WebBrowser Into a ComboBox
    Lets say this page contain a combobox that i want to get it items, how can we make it happen on the GetAttribute Method ?
    Well, sure i tried the following (which has no chance for a result) :

        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
            For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("select")
                If element.GetAttribute("name") = "cars" Then
                    ComboBox1.Items.Add(element.GetAttribute("value")) 'Probably here is the problem.
                End If
            Next
        End Sub

    What am i missing ?

    Thanks.


    Saturday, March 4, 2017 8:16 PM

Answers

  • Given

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    	<head>
    		<title></title>
    		<script>
    			function yearChanged() {
    				alert('Invoked onchange');
    			}
    		</script>
    	</head>
    	<body>
    		<select name = "yr" id="select_id" onchange="yearChanged()">
    			<option value= "">Year</option>
    			<option>2014</option>
    			<option>2015</option>
    			<option>2016</option>
    			<option>2017</option>
    		</select>	
    	</body>
    </html>

    We can get the option items in the select element

    Dim items =
        (
            From item In WebBrowser1.Document.GetElementsByTagName("select").Cast(Of HtmlElement)()
            Where item.GetAttribute("name") = "yr"
            Select item.GetElementsByTagName("option")
        ).FirstOrDefault
    
    
    Dim options As String = String.Join(",", (From X In items.Cast(Of HtmlElement)() Select X.InnerText).ToArray)
    MessageBox.Show(options)

    If we want to set an option

    Dim items As HtmlElementCollection =
        (
            From item In WebBrowser1.Document _
            .GetElementsByTagName("select").Cast(Of HtmlElement)()
            Where item.GetAttribute("name") = "yr"
            Select item.GetElementsByTagName("option")
        ).FirstOrDefault
    
    Dim ItemToSelect As HtmlElement =
        (
            From item In items.Cast(Of HtmlElement)()
            Where item.InnerText = "2017"
        ).FirstOrDefault
    
    ItemToSelect.SetAttribute("selected", CStr(True))
    
    items(0).Parent.RaiseEvent("onchange")


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Zuher Laith Monday, March 6, 2017 5:01 PM
    Saturday, March 4, 2017 9:00 PM
    Moderator

All replies

  • Given

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    	<head>
    		<title></title>
    		<script>
    			function yearChanged() {
    				alert('Invoked onchange');
    			}
    		</script>
    	</head>
    	<body>
    		<select name = "yr" id="select_id" onchange="yearChanged()">
    			<option value= "">Year</option>
    			<option>2014</option>
    			<option>2015</option>
    			<option>2016</option>
    			<option>2017</option>
    		</select>	
    	</body>
    </html>

    We can get the option items in the select element

    Dim items =
        (
            From item In WebBrowser1.Document.GetElementsByTagName("select").Cast(Of HtmlElement)()
            Where item.GetAttribute("name") = "yr"
            Select item.GetElementsByTagName("option")
        ).FirstOrDefault
    
    
    Dim options As String = String.Join(",", (From X In items.Cast(Of HtmlElement)() Select X.InnerText).ToArray)
    MessageBox.Show(options)

    If we want to set an option

    Dim items As HtmlElementCollection =
        (
            From item In WebBrowser1.Document _
            .GetElementsByTagName("select").Cast(Of HtmlElement)()
            Where item.GetAttribute("name") = "yr"
            Select item.GetElementsByTagName("option")
        ).FirstOrDefault
    
    Dim ItemToSelect As HtmlElement =
        (
            From item In items.Cast(Of HtmlElement)()
            Where item.InnerText = "2017"
        ).FirstOrDefault
    
    ItemToSelect.SetAttribute("selected", CStr(True))
    
    items(0).Parent.RaiseEvent("onchange")


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Zuher Laith Monday, March 6, 2017 5:01 PM
    Saturday, March 4, 2017 9:00 PM
    Moderator
  • We can get the option items in the select element
    Dim items =
        (
            From item In WebBrowser1.Document.GetElementsByTagName("select").Cast(Of HtmlElement)()
            Where item.GetAttribute("name") = "yr"
            Select item.GetElementsByTagName("option")
        ).FirstOrDefault
    
    
    Dim options As String = String.Join(",", (From X In items.Cast(Of HtmlElement)() Select X.InnerText).ToArray)
    MessageBox.Show(options)

    Well, it looks make sense as a code, but it gave me an error at the options variable, and says "Value cannot be null."
    The Page I'm trying to use the code on it is here
    I don't get it really ..

    Sunday, March 5, 2017 10:49 AM
  • Try the following

    Public Class CarsForm
        Private Sub CarsForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim htmlCode =
                    <html>
                        <body>
                            <form action="/action_page.php">
                                <select name="cars">
                                    <option value="volvo">Volvo</option>
                                    <option value="saab">Saab</option>
                                    <option value="fiat">Fiat</option>
                                    <option value="audi">Audi</option>
                                </select>
                                <input type="submit"></input>
                            </form>
    
                        </body>
                    </html>
            WebBrowser1.DocumentText = htmlCode.ToString
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
            Handles WebBrowser1.DocumentCompleted
    
            Dim items As HtmlElementCollection =
                (
                    From item In WebBrowser1.Document.Forms(0) _
                    .GetElementsByTagName("select").Cast(Of HtmlElement)()
                    Where item.GetAttribute("name") = "cars"
                    Select item.GetElementsByTagName("option")
                ).FirstOrDefault
    
            Dim ItemToSelect =
                (
                    From item In items.Cast(Of HtmlElement)()
                    Where item.InnerHtml = "Fiat"
                ).FirstOrDefault
    
            If ItemToSelect IsNot Nothing Then
                ItemToSelect.SetAttribute("selected", CStr(True))
            End If
    
        End Sub
    End Class

    The difference here is the form in the html. Now what is above is brittle meaning if the elements in question where in a different form we would need to consider this and iterate all forms which would be similar logic to find anything on a page.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 5, 2017 11:32 PM
    Moderator