本文實例講述了用WebBrowser操作frame和iframe的方法,比較適合C#初學者參考學習。示例淺顯易懂,具體方法如下:
1.獲取frame的源文件
MessageBox.Show(webBrowser1.Document.Window.Frames["main"].Document.Body.InnerHtml);
2.獲取frame的HTMLDocument接口
HTMLDocument doc = (HTMLDocument)webBrowser1.Document.DomDocument;object j;for (int i = 0; i < doc.parentWindow.frames.length; i++){ j = i; HTMLWindow2Class frame = doc.parentWindow.frames.item(ref j) as HTMLWindow2Class; if (frame.name == "main") { MessageBox.Show(frame.document.title); }}
3.獲取frame的IHTMLDocument2接口
IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.Window.Frames["main"].Document.DomDocument;
4.取得frame中被點擊的連接
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e){ string url = webBrowser1.Document.Window.Frames["main"].Document.ActiveElement.GetAttribute("src");}
希望本文所述實例對大家的C#學習能起到一定的幫助作用。
新聞熱點
疑難解答
圖片精選