Trigger OnChange event of a combo box on a page in a WebBrowser

B

BobRoyAce

There is a web page that I am doing some automation for, using a
WebBrowser on a form. It has a combo box as follows:

<select name="ddlMonth" onchange="javascript:setTimeout('__doPostBack
(\'ddlMonth\',\'\')', 0)" language="javascript" id="ddlMonth"
onkeypress="if (event.keyCode == 13) return false;">
<option selected="selected" value="2/1/2009">February 2009</option>
<option value="1/1/2009">January 2009</option>
</select>

I can easily change the displayed/selected item in the combo box as
follows:

Dim oComboBoxElement As HtmlElement = WebBrowser1.Document.All
("ddlMonth")

For Each oComboBoxItem As HtmlElement In oComboBoxElement.Children
If (oComboBoxItem.GetAttribute("value") = "1/1/2009") Then
oComboBoxItem.SetAttribute("selected", "x")
End If
Next

However, doing it this way, apparently, does not trigger the onchange
event, something I need to do for the automation to work, as the
onchange event fires off code . How can I change the selected item
while, at the same time, triggering the onchange event to fire?
 
B

BobRoyAce

I figured out a way to do it...

WebBrowser1.Document.Forms(0).InvokeMember("submit")
 
Joined
May 28, 2009
Messages
1
Reaction score
0
I found a better solution if you ever need in future :

Make sure that you declare oComboBoxElement As mshtml.HTMLSelectElement
Then:

oComboBoxElement.FireEvent("onchange")

List of other events can be found in list of properties for object.

Cheers,
Johal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top