(1)打开并操作外部IE,这个只有MSHTML能做到:
打开ie:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
object objFlags = 1;
object objTargetFrameName = "";
object objPostData = "";
object objHeaders = "";
SHDocVw.InternetExplorer webBrowser1= (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count-1);
webBrowser1.Navigate(“http://www.google.cn”, ref objFlags, ref objTargetFrameName, ref objPostData, ref objHeaders);
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;
IHTMLElementCollection eCollection = doc.getElementsByTagName("input");
foreach (IHTMLElement element in eCollection)
{
if(element.getAttribute("name")=="username")
element.innerHTML = name.Trim();
}
---------------------------------------------------------------------
HtmlElementCollection submits = webBrowser1.Document.GetElementsByTagName("select");
foreach (HtmlElement submit in submits)
{
if (submit.GetAttribute("name") == "typeid")
{
submit.SetAttribute("value", siteid);
//submit.InvokeMember("click");
break;
}
}
(3)要点击里面的某个按钮
mshtml.IHTMLDocument2 doc2=(mshtml.IHTMLDocument2)myWeb.Document;
mshtml.IHTMLElementCollection inputs;
inputs=(mshtml.IHTMLElementCollection)doc2.all.tags("INPUT");
mshtml.IHTMLElement element=(mshtml.IHTMLElement)inputs.item("SubmitBut",0);
element.click();
------------------------------------------------------
HtmlElement loginsubmit = webBrowser1.Document.GetElementById("loginsubmit");
loginsubmit.InvokeMember("click");
(4)根据元素ID获取元素的值。
比如要获取<img class="" id="regimg" src="/register/checkregcode.html?1287068791" width="80" height="22">这个标签里的src属性的值:
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser1.Document;
mshtml.IHTMLElement img = (mshtml.IHTMLElement)doc2.all.item("regimg", 0);
string imgUrl = (string)img.getAttribute("src");
----------------------------------------------------------------
HtmlElement regbimp1 = webBrowser1.Document.GetElementById("regimg");
string imgUrl = (string)img.getAttribute("src");
(5)执行JS
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
win.execScript("changeRegImg()", "javascript");//使用JS
-------------------------
IHTMLWindow2 login = (mshtml.IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
login.execScript("updateseccode()", "javascript");
(6)、禁止JS,WPF下目前发现唯一适用的一种方法:
public void HideScriptErrors(WebBrowser wb, bool Hide)
{
FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiComWebBrowser == null) return;
object objComWebBrowser = fiComWebBrowser.GetValue(wb);
if (objComWebBrowser == null) return;
objComWebBrowser.GetType().InvokeMember(
"Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
}
void webBrowser1_Navigated(object sender, NavigationEventArgs e)
{
HideScriptErrors(webBrowser1,
true);
}

本文介绍了如何利用MSHTML接口与System.Windows.Forms.HtmlElement来操作WebBrowser控件,包括打开网页、操作HTML元素、点击按钮、获取属性值以及执行JavaScript。示例代码详细展示了如何与网页元素交互,如设置表单字段、调用JS函数以及禁用JavaScript错误显示。
2687

被折叠的 条评论
为什么被折叠?



