Web 浏览器控制文档似乎不完整



我在程序中使用Web浏览器控件,但是当导航到站点后,我尝试使用Web浏览器的文档(如getelementbyid ... )获取一些元素,我注意到缺少一些元素。

我知道元素是由一些JavaScript动态插入到页面中的。

我搜索了获取这些元素的方法,并尝试让他们注入要在页面中执行的 JavaScript 并通过 window.external 方法返回一些元素(或只是通过警报尝试),但即使执行此脚本,它们也只是返回与原始代码中文档上的方法相同的结果。

有一种方法可以以某种方式访问我的程序中的这个"不可见"元素,就像我在 Internet Explorer 中按 F12 来访问它们一样?谢谢你的回答,对不起我的床英语。

这是我的代码:

        private void button2_Click(object sender, EventArgs e)
    {
        //injecting and executing my javascript code
        HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
        IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
        element.text = "function sayHello() {  window.external.f1(document.getElementsByTagName('html')[0].innerHTML); }";
        head.AppendChild(scriptEl);
        webBrowser1.Document.InvokeScript("sayHello");

        String  v;
        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\test.txt");
        foreach (HtmlElement k in webBrowser1.Document.GetElementsByTagName("html"))
        {
          v=  k.OuterHtml;
          file.WriteLine(v);
        }
        file.Close();
    }
    [ComVisible(true)]
    public class MyScript {
    //function called by the javascript    
    public void f1(string s)
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\test2.txt");
        file.WriteLine(s);
        file.Close();
    }
    }

在执行结束时,Test和test2都具有相同的html,因为缺少某些元素。

你应该在你的问题中更具体,并在这里传递你遇到问题的代码。也许您没有访问文档的正确部分

相关内容

最新更新