问题描述
由于这种方法在一个网页浏览器HTML页面上工作:
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;
}
我需要了解为什么这个code运行就像一个魅力会显示消息框时,当它被注释掉了没有。
我怀疑在这些问题被resumend:
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)是如何code的流量时,消息框是程序的一部分,当它是不是?我的意思是在code堵住了用户presses好吗?
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)I期号上述3大火一些JavaScript在不发出导航事件(因此也没有DocumentCompleted页),但可以访问一些隐藏的HTML无法到达没有点击一个的标签。在实践中,它只是改变标签的innerHTML,它创建一个表单。
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)我试图实现几个解决方案阶段4,如下所示起来,ThreadSleep(一个消息框),一个SpinWait(),甚至一个for循环搞乱了一切,但所有这些解决方案似乎并没有让web浏览器程序在可视化屏幕上的形式。仅在消息框带来它到屏幕上,即使该用户是在pressing非常快速确定并关闭它
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)我需要找到不涉及外部(用户)输入解决方案(例如在MessageBox,以便等待出现completerly装载在屏幕上的形式被关闭),但没有事件来帮忙。
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.
更多的数据来评估的情况下:
- 在code我写的是良好的目标,我试图把它拆分成3个按键来管理手中的时间和它工作正常。
- 完成了文档不能被用于code组之间的切换,因为大约有300页的自动化,每个页面都可以有10-15的方法来自动化他们,这是不可能来管理所有这些单一的事件处理程序,没有builind一个永无止境的开关结构。我会尝试如果可能的话牛逼避免它。
- 我已经找到了其他用户的一些有趣的问题,如以下,但没有我的情况的解决方案:
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与WebBrowser.IsBusy或readyState的(VB .NET)
Detect当AJAX在web浏览器一个DIV改变HTML
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
可能有人给我一个手。
对不起,这是我的第一个线程,希望我已经明确。 TKS
Sorry it is my first thread, hope I've been clear. Tks
推荐答案
我在这里张贴的解决方案,我已经能够找到这样的问题:我写的类型的HtmlElement扩展方法如下:
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
这篇关于网页浏览器:当没有DocumentCompleted被托管的网页的链接解雇测序activites的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!