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

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
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?
 
I figured out a way to do it...

WebBrowser1.Document.Forms(0).InvokeMember("submit")
 
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.
 
Back
Top