2012-04-17 78 views
0

我在我的程序中使用webbrowser控件,但是当浏览到一个网站后,我尝试使用webbrowser的文档(如getelementbyid ...)来获取一些元素,我注意到某些元素缺失。Webbrowser控件文档似乎不完整

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

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

有一种方法可以在我的程序中以某种方式访问​​此“隐形”元素,就像我通过在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(); 
    } 
    } 

在执行这两个测试结束和TEST2具有相同的HTML是一些元素缺失。

回答

0

你应该在你的问题中更具体一些,并在这里通过代码来解决问题。也许你没有访问文档的正确部分