I'm loading a web page into a WebBrowser
object, and part of the web page has an HTML Form
with 3 attributes, namely two Select
attributes and one Input
attribute of type Submit
. The two Select
attributes present different lists of choices, and having made a choice one presses the button that corresponds to the Input
attribute which causes data to be loaded. When the program is running and the WebBrowser
object is visible, I can manually use the mouse and the web page loads the data correctly, exactly as though it was running in a browser like e.g. Chrome. However, when I try to write C# to control the WebBrowser
object, it doesn't work.
When the web page is first loaded, the Input
attribute on the form is greyed out. However, using the mouse to make a choice in either of the two Select
attributes causes the Input
attribute to be enabled and it changes colour to green. The C# code I have to make a selection doesn't replicate that behaviour, because the Input
attribute doesn't get enabled as a result of the code (below) which does the selection. It's not clear to me where the code which causes the Input
attribute to be enabled is located, but I've been hoping that I don't need to work that out.
The HTML code for the form is as follows:
<form method="get" id="filter-nav">
<fieldset>
<label for="group-drop-down">Show me:</label>
<select id="drop-down-1" name="filter">
<option value="">drop-down-1-heading</option>
<optgroup label="optgroup-1-label" id="optgroup-1" >
<option value="optgroup-1-choice-1" > Choice-1 </option>
<option value="optgroup-1-choice-2" > Choice-2 </option>
</optgroup>
<optgroup label="optgroup-2-label" id="optgroup-2" >
<option value="optgroup-2-choice-3" > Choice-3 </option>
<option value="optgroup-2-choice-4" > Choice-4 </option>
</optgroup>
</select>
<span>OR</span>
<select id=""drop-down-2" name="filter">
<option value="">drop-down-2-heading</option>
<optgroup label="optgroup-3-label" id="optgroup-3" >
<option value="optgroup-3-choice-5" > Choice-5 </option>
<option value="optgroup-3-choice-6" > Choice-6 </option>
</optgroup>
<optgroup label="optgroup-4-label" id="optgroup-4" >
<option value="optgroup-4-choice-7" > Choice-7 </option>
<option value="optgroup-4-choice-8" > Choice-8 </option>
</optgroup>
</select>
<input id="filter-nav-submit" type="submit" value="Update" />
</fieldset>
</form>
and the C# code that I've been using to try and control it is the LoadData() method of the following class
private class WebBrowserHelper {
WebBrowser wb;
public void LoadData() {
HtmlElement select1 = getElement("select", "drop-down-1");
select1.Focus();
Application.DoEvents();
select1.SetAttribute("Value", "optgroup-1-choice-2");
Application.DoEvents();
select1.InvokeMember("change");
Application.DoEvents();
// at this point, the select1 dropdown shows the right value
// but the button corresponding to the input attribute is still greyed out
}
public HtmlElement getElement(string tagName, string IdName) {
HtmlElementCollection theElementCollection = wb.Document.GetElementsByTagName(tagName);
foreach (HtmlElement curElement in theElementCollection) {
object id = curElement.GetAttribute("id");
if (id != null && id.Equals(IdName)) return curElement;
}
return null;
}
}
What am I doing wrong?