简体   繁体   English

InvokeMember C#的无限循环

[英]Endless loop with InvokeMember c#

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. 我正在尝试标记一些复选框,然后使用Webbrowser提交表单,我成功了,问题是我的程序变得无穷无尽,它似乎是提交的表单,然后是提交之前的表单,然后是提交的表单,再次提交的表单以及提交之前的表单再次...再次...无限地如此。 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? 还是根据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. 没有更多的上下文我们无法真正分辨,但这就是为什么要设置这种行为。

Victor,尝试为提交操作使用其他形式(* .aspx)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM