1.脚本报错,设置属性不弹窗提示
webBrowser.ScriptErrorsSuppressed = true;
2.获取webbrowser 的document
项目中引用Com组件(MSHTML): Microsoft Http Object Library
using mshtml;
3.winform 中 执行脚本
//System.Windows.Forms 空间里的对象:HtmlDocument,HtmlElement
HtmlDocument hd= webBrowser.Document;
//以下为mshtml 空间里的对象:IHTMLWindow2,IHTMLDocument2,IHTMLElement
IHTMLDocument2 hd2= (IHTMLDocument2)hd.DomDocument;
IHTMLWindow2 hw2=hd2.parentWindow;
//执行脚本
hw2.execScript("document.getElementById('" + textBox2.Text + "').click();", "JavaScript");
//执行2 (将HtmlElement 装换成 IHtmlElement)
var e = hd.GetElementById(textBox2.Text);
e.Click += M_Click; //监控事件
var Ie = (IHTMLElement)e.DomElement;
Ie.click();