首页 > 代码库 > c# webBrowser全掌握
c# webBrowser全掌握
一、获取网页源代码
1.不含有框架
string s=WB1.DocumentText; //webbrowser1命名为WB1,下同
2.含有框架
引用mshtml; //位置C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll
object i_frame=0; //第一个框架IHTMLDocument2 doc = FWB.Document.DomDocument as IHTMLDocument2;
IHTMLFramesCollection2 frames = doc.frames as IHTMLFramesCollection2;
IHTMLWindow2 frame = frames.item(ref i_frame) as IHTMLWindow2;
IHTMLDocument2 frameDoc = frame.document as IHTMLDocument2;
string s=frameDoc.Document.body.innerHTML;
二、获取网页元素
1.根据ID
HtmlDocument ele= WB1.Document.getElementById("元素的ID");
三、执行JS函数
1.用Navigate
WB1.Navigate("javascript:postComment();"); //postComment为要执行的JS函数
2.用IhtmlWindow2接口
IHTMLWindow2 win2 = WB1.Document.Window.DomWindow as IHTMLWindow2;
win2.execScript("function confirm(){return true;}", "javascript");
3.用IhtmlDocument2接口的parentWindow
IHTMLDocument2 doc2 = WB1.Document.DomDocument as IHTMLDocument2;
doc2.parentWindow.execScript("function confirm() {return true;}", "javascript");
待续