Webbrowser:当托管网页上的链接触发DocumentCompleted时,排序活动 [英] Webbrowser: sequencing activites when no DocumentCompleted is fired by a link on hosted webpage

查看:131
本文介绍了Webbrowser:当托管网页上的链接触发DocumentCompleted时,排序活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这种方法在webbrowser中的HTML页面上工作:

  bool semaphoreForDocCompletedEvent; 

private void button12_Click(object sender,EventArgs e)
{
checkBox1.Checked = false; //取消选中NAvigating事件是否触发,并在DocumentCompleted被触发后检查,只有在Form
HtmlDocument doc = Program.wb.Document上有可视化引用。
HtmlElement ele = doc.GetElementById(menuTable);
foreach(元素中的HtmlElement子)
{
if(sub.GetAttribute(href)。包含(something))
{
ele = sub;
break;
}
}
// PHASE 1:点击Web链接导航到包含其他按钮和链接的页面对象obj = ele.DomElement;
System.Reflection.MethodInfo mi = obj.GetType()。GetMethod(click);
mi.Invoke(obj,new object [0]);
// PHASE 2:等待文件完成以确保文档已完全加载

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

ele = doc.GetElementByI(button1)。FirstChild.FirstChild.FirstChild.NextSibling;
// PHASE 3:点击Web按钮打开一个窗体

obj = ele.DomElement;
mi = obj.GetType()。GetMethod(click);
mi.Invoke(obj,new object [0]);
// PHASE 4:显示一个令人厌烦的用户的模式MEssageBox

if(checkBox1.Checked == false)
MessageBox.Show(nonc'èstato文件完成);
checkBox1.Checked = false;

// PHASE 5:提交表单(不需要填写任何输入)

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;
}
}

私人bool WaitForDocumentCompleted()
{
while(IsBusy)
{
Application.DoEvents() ;
Thread.SpinWait(1000);
}
返回true;
}

我需要了解为什么这个代码在显示消息框时像一个魅力一样运行并且当它被注释掉时。
我的疑问可以在这些问题中得到回复:



1)当消息框是程序的一部分时,代码的流程如何不?我的意思是代码阻止了用户按OK吗? 2)上面我用3号表示的阶段,在页面上发出一些不发布导航事件的JavaScript(因此没有DocumentCompleted),但是允许访问一些隐藏的HTML无法点击A标签即可访问。实际上,它只是改变一个标签的InnerHtml,在其中创建一个FORM。



3)我试图为第4阶段实现几个解决方案,一个如这里所示的消息框一个ThreadSleep(),一个SpinWait(),甚至一个for循环将所有的东西都弄乱了,但是所有这些解决方案似乎没有让Webbrowser进行可视化的屏幕显示。只有消息框才能显示屏幕,即使用户按OK也很快关闭。



4)我需要找到一个不是涉及外部(用户)输入(例如要关闭的消息框),以等待表单在屏幕上完全加载,但没有事件来帮助。



一些更多的数据来评估这个案例:
- 我写的代码对于目标是有好处的,我试图将它分成3个按钮来手工管理时间,它工作正常。
- 完成的文档不能用于代码拆分之间的切换,因为大约有300页自动化,每个页面都可以有10-15种自动化方法,无法为所有这些方法管理单个事件处理程序,而不需要建立一个不断变化的开关结构。如果可能,我会尽量避免。
- 我发现其他用户的一些有趣的问题,如下所示,但没有解决我的情况:



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



检测AJAX何时更改HTML中的HTML DIV in WebBrowser



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



有人可以给我一个手。



对不起,这是我的第一个线程,希望我已经清楚了。 Tks

解决方案

我在这里发布解决方案,我可以找到这个问题:我写了一个扩展方法HtmlElement类型如下:

  public static bool WaitForAvailability(此HtmlElement标记,字符串ID,HtmlDocument documentToExtractFrom,long maxCycles)
{
bool cond = true;长计数器= 0;
while(cond)
{
Application.DoEvents();
tag = documentToExtractFrom.GetElementById(id);
if(tag!= null)
cond = false;
Thread.SpinWait(50000);
counter ++;
if(counter> maxCycles)
return false;
}
返回true;
}

这样可以等待一段时间,在页面


中确实可用

Given this method to work on a HTML page in a webbrowser:

    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? 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. In practice it just changes the InnerHtml of a tag, creating a FORM in it.

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. 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.

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. - 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. 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)

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

Could somebody give me a hand.

Sorry it is my first thread, hope I've been clear. Tks

解决方案

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:

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

这篇关于Webbrowser:当托管网页上的链接触发DocumentCompleted时,排序活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆