在执行正文加载事件后,在WinForms WebBrowser中获取HTML正文内容

本文关键字:正文 获取 WebBrowser HTML 执行 加载 事件 WinForms | 更新日期: 2023-09-27 18:28:29

我在WinForms中有一个WebBrowser控件,其URL属性设置为外部网页。我还有一个DocumentCompleted事件的事件处理程序。在这个处理程序中,我试图获取特定的元素,但是wb。Document.Body似乎在执行onload之前捕获了HTML。

{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"

"<body onload='"evt_Login_onload(event);'" uitheme='"Web'">'n</body>"是JavaScript之前的内容。有没有办法在evt_Login_onload(event);执行后捕捉body标签的状态?

我也尝试过使用wb.Document.GetElementById("id"),但它返回null。

在执行正文加载事件后,在WinForms WebBrowser中获取HTML正文内容

以下是如何做到这一点,我在内联中添加了一些注释:

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

更新,现在是2019年,这个答案令人惊讶地仍然受到关注,所以我想注意,我建议使用现代C#的方法是使用async/await,就像这样。