IHTMLDocument2 vDocument = webBrowser.Document.DomDocument as IHTMLDocument2;
IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
// 注入function方法
// vWindow.execScript("ShowMessage(67890);", "JavaScript"); // 执行脚本
// vWindow.execScript("setValue(999);", "JavaScript"); // 执行脚本
// // 获取脚本返回的值
//string temp = vWindow.execScript("getValue();", "JavaScript");
// 获取控件的HtmlDocument,通过这个获取页面的一些值
HtmlDocument document = this.webBrowser.Document;
// 好像是和vWindow.execScript("getValue();", "JavaScript"); 相似
String s = (String)document.InvokeScript("getValue");
// 获取页面html代码,有一些\n\r
string strHtml = document.Body.InnerHtml;
☆下面是在网上找的,可能也好用,没有尝试
获取body内的html代码
//string html = document.body.innerHTML;
暂时没搞明白应该怎么获取所有的html代码
根据ID获取元素
根据id获得input
//IHTMLElement inputSearch = (IHTMLElement)doc.all.item("kw");
为input设置value属性
//inputSearch.setAttribute("value", "设置搜索内容");
获得搜索按钮
//IHTMLElement submitSearch = (IHTMLElement)doc.all.item("su");
点击按钮
//submitSearch.click();
获取窗体
//mshtml.IHTMLWindow2 window = (mshtml.IHTMLWindow2)doc.parentWindow;
还有一些别的,没整理。
private void SetWeb()
{
C#调用窗口
HTMLDocument doc = this.webBrowser1.Document as HTMLDocument;
//HtmlDocument main_formDoc = webBrowser1.Document;
//IHTMLDocument2 main_mshtmlIHTMLDoc = (IHTMLDocument2)main_formDoc.DomDocument;
//Object frame_index = 0;
//IHTMLWindow2 target_mshtmlIHTMLWindow = (IHTMLWindow2)main_mshtmlIHTMLDoc.frames.item(ref frame_index);
//IHTMLDocument2 target_mshtmlIHTMLDoc = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(target_mshtmlIHTMLWindow);
//HTMLDocument doc = target_mshtmlIHTMLDoc as HTMLDocument;
//mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
//win.execScript("changeRegImg()", "javascript");//使用JS
IHTMLDocument2 vDocument = webBrowser.Document.DomDocument as IHTMLDocument2;
IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
Type vWindowType = vWindow.GetType();
object testText = vWindowType.InvokeMember("testText",
BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取
Console.WriteLine(testText);
vWindowType.InvokeMember("testText",
BindingFlags.SetProperty, null, vWindow, new object[] { "Zswang 路过" }); // 设置
vWindowType.InvokeMember("ShowMessage",
BindingFlags.InvokeMethod, null, vWindow, new object[] { 12345 }); // 执行方法
//获取body内的html代码
//IHTMLElement inputSearch = (IHTMLElement)doc.
//inputSearch.setAttribute("value", "设置搜索内容");
//htmlText = webBrowser1.Document.Body.InnerHtml;
}