简体   繁体   English

Webbrowser:托管网页上的链接未触发DocumentCompleted时,排序激活

[英]Webbrowser: sequencing activites when no DocumentCompleted is fired by a link on hosted webpage

Given this method to work on a HTML page in a webbrowser: 使用此方法可以在Web浏览器的HTML页面上工作:

    bool semaphoreForDocCompletedEvent;

                private void button12_Click(object sender, EventArgs e)
                        {
                            checkBox1.Checked = false; //unchecked if the NAvigating event is fired and Checked after DocumentCompleted is fired, only to have a visual reference on the Form
                            HtmlDocument doc = Program.wb.Document;
                            HtmlElement ele = doc.GetElementById("menuTable");
                            foreach (HtmlElement sub in ele.All)
                            {
                                if (sub.GetAttribute("href").Contains("something"))
                                {
                                    ele = sub;
                                    break;
                                }
                            }
//PHASE 1: clicking on a Web link to navigate to a page that contains other buttons and links                       object obj = ele.DomElement;
                            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
                            mi.Invoke(obj, new object[0]);
//PHASE 2: Waiting for document completed in order to be sure the document is fully loaded

                            semaphoreForDocCompletedEvent = WaitForDocumentCompleted();
                            if (!semaphoreForDocCompletedEvent)
                                throw new Exception("casino in giro!");

                            ele = doc.GetElementByI("button1").FirstChild.FirstChild.FirstChild.NextSibling;
//PHASE 3: clicking on a Web button to open a form

                            obj = ele.DomElement;
                            mi = obj.GetType().GetMethod("click");
                            mi.Invoke(obj, new object[0]);
//PHASE 4: displaying a modal MEssageBox that annoy the user a lot

                            if (checkBox1.Checked == false)
                                MessageBox.Show("non c'è stato document completed");
                            checkBox1.Checked = false;

//PHASE 5: submitting the form (that does not need any imput to be filled in)

                            ele = doc.GetElementById("planet");
                            ele = ele.FirstChild.NextSibling.NextSibling;

                            obj = ele.DomElement;
                            mi = obj.GetType().GetMethod("submit");
                            mi.Invoke(obj, new object[0]);
                        }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                Program.toBox = Program.wb.Document.Body.InnerHtml.ToString();
                if (Program.wb.ReadyState == WebBrowserReadyState.Complete)
                {
                    checkBox1.Checked = true;
                    IsBusy = false;
                }
            }

        private bool WaitForDocumentCompleted()
                { 
                    while (IsBusy)
                    {
                        Application.DoEvents();
                        Thread.SpinWait(1000);
                    }
                    return true;
                }

I need to understand why this code runs like a charm when the messagebox is displayed and does not when it is commented out. 我需要了解为什么在显示消息框时此代码像超级按钮一样运行,而在注释掉后却不能运行。 My doubts can be resumend in these questions: 我的疑问可以在以下问题中再次提出:

1) how is the flow of the code when the message box is part of the program and when it is not? 1)当消息框是程序的一部分时,而不是消息框的一部分,代码的流程如何? I mean is the code blocked up to the user presses ok? 我的意思是代码被阻止,直到用户按下确定?

2) the phase I indicated above with number 3 fires some javascript in the page that does not issue a Navigating event (therefore no DocumentCompleted) but gives access to some hidden HTML not reachable without clicking on a A tag. 2)我在上面用数字3指示的阶段在页面中触发了一些javascript,这些JavaScript不会发出导航事件(因此没有DocumentCompleted),但是可以访问某些无法访问的隐藏HTML,而无需单击A标签。 In practice it just changes the InnerHtml of a tag, creating a FORM in it. 实际上,它只是更改标签的InnerHtml,并在其中创建FORM。

3) I tried to implement several solutions for phase 4, a Message box as indicated here up, a ThreadSleep(), a SpinWait() and even a for loop messing everything up, but all those solutions seem not to let the Webbrowser proceeding in visualizing the form on screen. 3)我尝试为阶段4实现几种解决方案,如此处所示的消息框,ThreadSleep(),SpinWait()甚至for循环将所有内容弄乱了,但是所有这些解决方案似乎都不允许Webbrowser继续进行在屏幕上可视化表单。 Only the message box brings it up to screen, even if the user is very fast in pressing OK and closing it. 即使用户快速按下“确定”并关闭它,也只有消息框将其显示在屏幕上。

4) I need to find a solution that does not involve external (user) input (such the Messagebox to be closed) in order to wait for the form to appear completerly loaded on the screen, but no events come to help. 4)我需要找到一个不涉及外部(用户)输入(例如要关闭的消息框)的解决方案,以等待表单完全加载到屏幕上,但是没有任何事件可以提供帮助。

Some more data to evaluate the case: - the code I wrote is good for the aim, I tried to split it into 3 buttons to manage the timing by hand and it works fine. 一些更多的数据可以评估这种情况:-我编写的代码很适合此目的,我尝试将其分成3个按钮来手动管理计时,并且效果很好。 - the document completed cannot be used for switching between code splits, as there are around 300 pages automated and each page can have 10-15 methods to automate them, it's impossible to manage a single eventhandler for all of them, without builind up a neverending Switch struct. -完成的文档不能用于在代码拆分之间进行切换,因为大约有300页是自动的,并且每页可以有10-15个方法来实现它们的自动化,因此如果不建立无休止的注释,就不可能为所有这些方法管理一个事件处理程序切换结构。 I would try t avoid it if possible. 如果可能的话,我会尽量避免。 - i've found some interesting issues of other users like the following but without solution for my case: -我发现了其他用户的一些有趣问题,如下所示,但我的案例没有解决方案:

InvalidCastException with WebBrowser.IsBusy or ReadyState (VB .NET) WebBrowser.IsBusy或ReadyState(VB .NET)的InvalidCastException

Detect when AJAX changes HTML in a DIV in WebBrowser 检测AJAX何时更改WebBrowser中DIV中的HTML

http://www.techtalkz.com/vb-net/374234-vb-net-webbrowser-control-how-capture-javascript-events-statusbar-changed-mouseclick-etc.html http://www.techtalkz.com/vb-net/374234-vb-net-webbrowser-control-how-capture-javascript-events-statusbar-changed-mouseclick-etc.html

Could somebody give me a hand. 有人可以帮我一下。

Sorry it is my first thread, hope I've been clear. 抱歉,这是我的第一个话题,希望我已经清楚了。 Tks ks

I'm posting here the solution i've been able to find for this problem: I wrote an extension method for the HtmlElement type as follows: 我在这里发布了我已经可以找到的解决方案:我为HtmlElement类型编写了一个扩展方法,如下所示:

public static bool WaitForAvailability(this HtmlElement tag, string id, HtmlDocument   documentToExtractFrom, long maxCycles)
{ 
    bool cond = true; long counter = 0; 
        while (cond) 
        { 
        Application.DoEvents(); 
        tag = documentToExtractFrom.GetElementById(id);
         if (tag != null) 
            cond = false;
         Thread.SpinWait(50000);
         counter++; 
        if (counter > maxCycles) 
            return false; 
        } 
    return true;
 }

This allows the required tag to be waited for up to the moment when it will be really available in the page 这样就可以等待所需的标签,直到它在页面中真正可用为止

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

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