使用WebBrowser访问DOM

Abu*_*fiz 9 .net c# asp.net dom webbrowser-control

我需要在页面上执行javascript后访问HTML文档的DOM.我有以下代码连接到URL并获取文档.问题是它在用javascript修改后永远不会得到DOM

public class CustomBrowser
{
    public CustomBrowser()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    protected string _url;
    string html = "";
    WebBrowser browser;

    public string GetWebpage(string url)
    {
        _url = url;
        // WebBrowser is an ActiveX control that must be run in a
        // single-threaded apartment so create a thread to create the
        // control and generate the thumbnail
        Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        string s = html;
        return s;
    }

    protected void GetWebPageWorker()
    {
        browser = new WebBrowser();
        //  browser.ClientSize = new Size(_width, _height);
        browser.ScrollBarsEnabled = false;
        browser.ScriptErrorsSuppressed = true;
        //browser.DocumentCompleted += browser_DocumentCompleted;
        browser.Navigate(_url);

        // Wait for control to load page
        while (browser.ReadyState != WebBrowserReadyState.Complete)
            Application.DoEvents();

        Thread.Sleep(5000);


        var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)browser.Document.DomDocument;

        html = documentAsIHtmlDocument3.documentElement.outerHTML; 


        browser.Dispose();
    }


}
Run Code Online (Sandbox Code Playgroud)

来自谷歌Chrome开发人员工具的DOM

我在我的代码中得到的DOM

我希望有人可以帮我解决这个问题

小智 1

检查页面在 IE7 中的呈现方式。我猜你缺少的标签是用jQuery添加的,并且页面上的jQuery版本2.2.4不支持IE7。我认为 WebBrowser 类确实围绕 IE7,即使您的 PC 上有较新版本的 IE。

如果您拥有该页面,请尝试添加 jQuery 迁移插件。