I am working on a simple application that automatically browses in a page that contains two dropdown menus and a button. The page looks like this:
------DropDown1-------
------DropDown2-------
-------Button---------
Now, the problem is, the contents of DropDown2
is dynamically generated by the selection of Dropdown1
.
I wrote code like this in c#:
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement elem = webBrowser1.Document.GetElementById("DropDown1");
elem.SetAttribute("selectedIndex", "1");
elem.RaiseEvent("onChange");
HtmlElement elem = webBrowser1.Document.GetElementById("DropDown2");
elem.SetAttribute("selectedIndex", "5");
elem.RaiseEvent("onChange");
}
After raising the onChange
event, the browser loads the new values but I can't get and set the DropDown2
value because the document still thinks DropDown2
s values are empty.
How can I get and set the new values that are generated in DropDown2
?
i have found the solution by invoking "__doPostBack" script after raising onChange event. when i invove the doPostBack, the document reloads and so i can retrieve the new values. heres the code:
private void BeginOperation()
{
webBrowser1.Navigate("somewebpage", false);
Task = 0;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement elem;
switch (Task)
{
case 0:
//HtmlDocument mydoc = webBrowser1.Document;
elem = webBrowser1.Document.GetElementById("ddlCity");
MessageBox.Show(elem.All.Count.ToString());
elem.SetAttribute("selectedIndex", "1");
//elem.RaiseEvent("onChange");
object[] args = {"someparameters"};
webBrowser1.Document.InvokeScript("__doPostBack",args);
Task++;
break;
case 1:
elem = webBrowser1.Document.GetElementById("ddlDistrict");
elem.SetAttribute("selectedIndex", "2");
elem.RaiseEvent("onChange");
object[] args2 = {"someparameters"};
webBrowser1.Document.InvokeScript("__doPostBack",args2);
Task++;
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With