I'm trying to mark some checkboxes and then submit a form using webbrowser, I succeeded, the problem is that my program is getting endless, It appears the form submitted, and then my form before being submitted, again submitted and again before being submitted and again...again....again.... infinitely so. I do not understand why.
I have realized that it happens in this part:
form.InvokeMember ("submit");
This is my code
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.mysite.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString() + " loaded";
webBrowser1.Document.GetElementById("check1").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check2").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check3").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check4").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check5").InvokeMember("CLICK");
foreach (HtmlElement form in webBrowser1.Document.Forms)
{
if (form.Name == "SearchForm")
{
form.InvokeMember("submit");
}
}
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.Text = "Navigating";
}
Well you've said that any time you finish loading a page, you want to submit the search form. So when that submit finishes, it will finish loading the page... and you'll trigger the same event. It's an entirely natural loop.
You should ask yourself what condition you want to detect that means you should submit the form when the page has finished loading. Should it only be when there isn't already something in the search box, for example? Or make it change based on the URL? Or perhaps you simply want to unhook the event handler for "document completed" before you submit the form? We can't really tell without more context, but that's why you're setting that behaviour.
See more on this question at Stackoverflow