IE浏览器自动点击等事件WebBrowser和mshtml.IHTMLDocument2(vb.net可参考)
(2012-09-28 21:30:55)
标签:
webbrowserihtmldocument2mshtml杂谈 |
分类: VB.NET编程 |
可以实现例如通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;可以自动点击网页上的按钮等功能
(要引入Microsoft.mshtml.dll 地址是C:\Program
Files\Microsoft.NET\Primary Interop Assemblies)
简单来说:
打开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);
(可以简略点写:object c=null;
myWeb.Navigate("http://zhidao.baidu.com/",ref
mshtml.IHTMLDocument2 htmlDoc = webBrowser1.Document as
mshtml.IHTMLDocument2;
//...获取WebBroswer中的body代码
mshtml.HTMLDocumentClass
doc=(mshtml.HTMLDocumentClass)myWeb.Document;
mshtml.HTMLBody body=(mshtml.HTMLBody)docCC.body;
string html=body.innerHTML.ToString();
//...如果里面有Form,要给里面的text填充信息
mshtml.IHTMLDocument2
doc2=(mshtml.IHTMLDocument2)myWeb.Document;
mshtml.IHTMLElementCollection inputs;
inputs=(mshtml.IHTMLElementCollection)doc2.all.tags("INPUT");
mshtml.IHTMLElement
element=(mshtml.IHTMLElement)inputs.item("userName",0);
mshtml.IHTMLInputElement
inputElement=(mshtml.IHTMLInputElement)element;
inputElement.value="填充信息";
//...要点击里面的某个按钮
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();
1、根据元素ID获取元素的值。
比如要获取这个标签里的src属性的值:
mshtml.IHTMLDocument2 doc2 =
(mshtml.IHTMLDocument2)webBrowser1.Document;
mshtml.IHTMLElement img =
(mshtml.IHTMLElement)doc2.all.item("regimg", 0);
string imgUrl = (string)img.getAttribute("src");
2、填写表单,并确定
mshtml.IHTMLElement loginname =
(mshtml.IHTMLElement)doc2.all.item("loginname", 0);
3、获取源码
textBox1.Text = doc2.body.innerHTML;
4、执行JS
mshtml.IHTMLWindow2 win =
(mshtml.IHTMLWindow2)doc2.parentWindow;
win.execScript("changeRegImg()", "javascript");//使用JS
5、禁止JS,WPF下目前发现唯一适用的一种方法:
public void
HideScriptErrors(WebBrowser wb, bool Hide)
下面是另外一遍博客里写的比较好的
#region
Search