获取HTML正文内容的WinForms WebBrowser控件身体的onload事件执行后, [英] Getting HTML body content in WinForms WebBrowser after body onload event executes
问题描述
我在的WinForms其URL属性设置为一个外部网页上的WebBrowser控件。我也有对DocumentCompleted事件的事件处理。这里面的处理程序,我试图让特定的元素,但wb.Document.Body似乎被执行的onload之前捕捉到HTML。
I have a WebBrowser control in WinForms whose URL property is set to an external webpage. I also have an event handler for the DocumentCompleted event. Inside this handler, I'm trying to get specific elements, but wb.Document.Body seems to capture the HTML before onload is executed.
{System.Windows.Forms.HtmlElement}
All: {System.Windows.Forms.HtmlElementCollection}
CanHaveChildren: true
Children: {System.Windows.Forms.HtmlElementCollection}
ClientRectangle: {X = 0 Y = 0 Width = 1200 Height = 0}
Document: {System.Windows.Forms.HtmlDocument}
DomElement: {mshtml.HTMLBodyClass}
ElementShim: {System.Windows.Forms.HtmlElement.HtmlElementShim}
Enabled: true
FirstChild: null
htmlElement: {mshtml.HTMLBodyClass}
Id: null
InnerHtml: "\n"
InnerText: null
Name: ""
NativeHtmlElement: {mshtml.HTMLBodyClass}
NextSibling: null
OffsetParent: null
OffsetRectangle: {X = 0 Y = 0 Width = 1200 Height = 0}
OuterHtml: "<body onload=\"evt_Login_onload(event);\" uitheme=\"Web\">\n</body>"
OuterText: null
Parent: {System.Windows.Forms.HtmlElement}
ScrollLeft: 0
ScrollRectangle: {X = 0 Y = 0 Width = 1200 Height = 0}
ScrollTop: 0
shimManager: {System.Windows.Forms.HtmlShimManager}
ShimManager: {System.Windows.Forms.HtmlShimManager}
Style: null
TabIndex: 0
TagName: "BODY"
&LT;身体的onload = \\evt_Login_onload(事件); \\uitheme = \\网络\\&GT; \\ n&LT; / BODY&gt;中
为pre-JavaScript内容。是否有办法在 evt_Login_onload(事件),以捕捉身体标签的状态;
执行
"<body onload=\"evt_Login_onload(event);\" uitheme=\"Web\">\n</body>"
is the pre-JavaScript content. Is there a way to capture the state of the body tag after evt_Login_onload(event);
executes?
我也尝试使用 wb.Document.GetElementById(ID)
,但它返回null。
I have also tried using wb.Document.GetElementById("id")
, but it returns null.
推荐答案
下面是如何是可以做到的,我已经把一些评论在线:
Here is how it can be done, I've put some comments inline:
private void Form1_Load(object sender, EventArgs e)
{
bool complete = false;
this.webBrowser1.DocumentCompleted += delegate
{
if (complete)
return;
complete = true;
// DocumentCompleted is fired before window.onload and body.onload
this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
{
// Defer this to make sure all possible onload event handlers got fired
System.Threading.SynchronizationContext.Current.Post(delegate
{
// try webBrowser1.Document.GetElementById("id") here
MessageBox.Show("window.onload was fired, can access DOM!");
}, null);
});
};
this.webBrowser1.Navigate("http://www.example.com");
}
这篇关于获取HTML正文内容的WinForms WebBrowser控件身体的onload事件执行后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!