Skip to main content
 首页 » 编程设计

c#之网络浏览器 : sequencing activites when no DocumentCompleted is fired by a link on hosted webpage

2024年01月02日40sharpest

鉴于此方法可以在网络浏览器中处理 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; 
                } 

我需要理解为什么这段代码在显示消息框时运行起来像一个魅力,而在它被注释掉时却没有。 我的疑惑可以在这些问题中恢复:

1) 当消息框是程序的一部分和不是程序的一部分时,代码流如何?我的意思是代码被阻止到用户按下 ok 吗?

2) 我在上面用数字 3 指出的阶段在页面中触发了一些 javascript,这些 javascript 不发出导航事件(因此没有 DocumentCompleted),但可以访问一些隐藏的 HTML,如果不单击 A 标记则无法访问。实际上,它只是更改标记的 InnerHtml,在其中创建一个 FORM。

3) 我尝试为第 4 阶段实现几个解决方案,一个消息框,如上所示,一个 ThreadSleep(),一个 SpinWait(),甚至一个 for 循环把一切都搞砸了,但所有这些解决方案似乎都没有让Web 浏览器继续在屏幕上可视化表单。只有消息框将其显示在屏幕上,即使用户非常快速地按下“确定”并关闭它也是如此。

4) 我需要找到一个不涉及外部(用户)输入(例如要关闭的消息框)的解决方案,以便等待表单完全加载到屏幕上,但没有任何事件可以提供帮助。

一些更多的数据来评估案例: - 我写的代码很适合这个目标,我试着把它分成 3 个按钮来手动管理时间并且它工作正常。 - 完成的文档不能用于在代码拆分之间切换,因为大约有 300 个页面被自动化,每个页面可以有 10-15 个方法来自动化它们,不可能为所有这些管理一个事件处理程序,而不建立一个永无止境的开关结构。如果可能的话,我会尽量避免它。 - 我发现了其他用户的一些有趣问题,如下所示,但我的情况没有解决方案:

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

Detect when AJAX changes HTML in a DIV in WebBrowser

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

谁能帮帮我。

抱歉,这是我的第一个帖子,希望我已经说清楚了。谢谢

请您参考如下方法:

我在这里发布了我能够找到的针对此问题的解决方案:我为 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; 
 } 

这允许等待所需的标记,直到它在页面中真正可用为止