Share via


Vb.net - How to select option in drop down menu using WebBrowser

Question

Wednesday, January 1, 2014 4:10 PM

Hi!

i'm using WebBrowser in vb.net for navigate this website: trakt.tv

How can i select the word "movies" in the drop down menu(top-right)?

This doesn't work:

WebBrowser1.Document.All("search-type").SetAttribute("Value", "movies")

Using WebKitBrowser, instead of WebBrowser, with this code:

WebKitBrowser1.StringByEvaluatingJavaScriptFromString("document.getElementById('search-type').options[4].selected = true;")

It Works! but i need to use WebBrowser.

All replies (6)

Wednesday, January 1, 2014 4:51 PM | 1 vote

Hello,

Here is a demo that sets a drop-down and some input elements.

Add a new resource to the project named BrowserText and insert the following.

I used a resource but you can do this from loading the WebBrowser with the content of a web page local or remote to get the same thing.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>Demo</title>
</head>
<body>
<H3>Example Reply Form</H3>
<FORM ACTION="mailto:[email protected]" METHOD="POST" ENCTYPE="text/plain">
    <P>
        <INPUT TYPE="hidden" NAME="subject" VALUE="Example Form">
        <SELECT NAME="age" id="MyAge">
            <OPTION VALUE="under 18">under 18 </OPTION>
            <OPTION SELECTED VALUE="18 to 25">18 to 25 </OPTION>
            <OPTION VALUE="25 to 30">25 to 30 </OPTION>
            <OPTION VALUE="30 to 40">30 to 40 </OPTION>
            <OPTION VALUE="over 40">over 40 </OPTION>     
        </SELECT> 
        Your age<BR>
        <INPUT TYPE="text" id="fName" NAME="name" size=40> Your name<BR>
        <INPUT TYPE="text" NAME="email" size=40> Your e-mail
    </P>
    <P>
        <TEXTAREA id="message" NAME="details" COLS=50 ROWS=10 WRAP="physical">Something about yourself</TEXTAREA>
    </P>

</FORM> 
</body>
</html>

Form woith a WebBrowser and Button

Public Class Form1
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        WebBrowser1.DocumentText = My.Resources.BrowserText
        While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        End While
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.GetElementById("fName").SetAttribute("value", TextBox2.Text)
        WebBrowser1.Document.GetElementById("message").SetAttribute("value", TextBox1.Text)

        Dim AgeElement As HtmlElement = WebBrowser1.Document.GetElementById("MyAge")
        Dim OldieOption As HtmlElement = AgeElement.GetElementsByTagName("option").Cast(Of HtmlElement).First(
            Function(el) el.GetAttribute("value") = "over 40")

        OldieOption.SetAttribute("selected", "true")

    End Sub
End Class

Before executing code in Button1

After executing code in Button1

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.


Wednesday, January 1, 2014 5:38 PM

Thanks for the help, but it still doesn't work.

Here my code:

'Set the word a in the search box
WebBrowser1.Document.All("q").SetAttribute("Value","a") 

'your code
Dim AgeElement As HtmlElement = WebBrowser1.Document.GetElementById("search-type")

Dim OldieOption As HtmlElement = AgeElement.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.GetAttribute("value") = "movies")

OldieOption.SetAttribute("selected", "true")


'click the search button
WebBrowser1.Document.Forms("global-search-form").InvokeMember("submit")

The drop down menu still show "All"


Wednesday, January 1, 2014 7:50 PM

In regards to the code I provided, it works just not for your specific page which is most likely caused by an element is not present at the time you attempted to set the dropdown.

For example, I once tinkered with Google to alter elements and had to use assertion to see if the form I wanted existed at that time, if it did not then it failed. On the surface all appeared fine yet dependent on conditions Google would not change the UI but did under the UI do a responsive change. WIth that, under one condition the form name was 'f' and another condition 'q'.

So you might use this WebBrowser1.Document.GetElementsByTagName("form") say in a LINQ statement to get form names, see if your form exists and the casing is the same as you are trying. Pass this I have no other suggestions.

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.


Wednesday, January 1, 2014 8:52 PM

Thanks, but seems a bit complicated for me.
Is it possible convert this code:

WebKitBrowser1.StringByEvaluatingJavaScriptFromString("document.getElementById('search-type').options[4].selected = true;")

into one using WebBrowser tool?
Because using WebkitBrowser works fine, but i need to use WebBrowser


Wednesday, January 1, 2014 9:22 PM

Hello,

Never tinkered with WebKitBrowser so I don't know.

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.


Tuesday, September 8, 2015 10:15 PM

When working with visual basic, its best to plan out how things will work, and how your coding will be layed out.

First of all, when working with select & option elements you need to know if your changing the select or option element.

You have to change the option element to a selected "True" value to select your value.. But how do you?

Here's a way I did it, I hope this helps

'Declare a variable and make sure you declare it as a HtmlElementCollectionDim option_ As HtmlElementCollection'Declare that option_ equals as a tag name of "option"
option_ = WebBrowser1.document.getElementByTagName("option")
'For each "option" element in the document
For Each option__ As HtmlElement In option_
'If option__ innerHtml contains a value then
if option__.InnerHtml = "April" then
'Once the value is founed, then get that element, and set the attribute of selected to true
option__.setAttribute("selected", True)

End If

NextOR you can do it this way'For each "option" element in the document
For Each option__ As HtmlElement In option_if InStr(option__.getAttribute("value").ToString, "April") thenoption__.setAttribute("selected, True)

End If

Next