下面的代码参考/kissdodog/p/3725774.html,非我原创,所以就标为转载
注意:下面的方法只适用于系统自带的IE浏览器,其它浏览器不适用,连360浏览器都不行
下面的程序实现三个功能
1 程序打开浏览器,并转到对应url的网页
2 获取浏览器地址栏上所有的url地址
3 模拟百度输入搜索
步骤如下:
1 新建一个控制台项目,名为'操作ie浏览器'
2 为项目添加Microsoft Internet Controls.dll引用和Microsoft HTML Object Library引用,注意Microsoft Internet Controls.dll引用被添加后还要鼠标右击它,设置它的 ‘嵌入互操作类型’ 属,性为False,不然会报错
3 添加一个类,名为IEOperation,并编辑如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 操作ie浏览器{class IEOperation{/// <summary>/// 利用ie浏览器打开url地址,如果ie浏览器处于关闭状态,则会打开ie浏览器并切换到url页/// </summary>/// <param name="url">要打开的url地址</param>public void OpenUrl(string url) {//新建一个Tab,然后打开指定地址(方式1)//SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();// SHDocVw.InternetExplorer webBrowser1 = (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count - 1);// webBrowser1.Navigate(url);//System.Diagnostics.Process.Start("iexplore.exe"); //直接打开IE浏览器(打开默认首页)System.Diagnostics.Process.Start("iexplore.exe",url); //直接打开IE浏览器,打开指定页}/// <summary>/// 获取当前浏览器打开的所有url地址/// </summary>/// <returns>得到的url集合</returns>public List<string> GetUrlList() {System.Diagnostics.Process []process=System.Diagnostics.Process.GetProcesses();List<string> UrlList = null;//证明没有启动浏览器,为了看到效果,默认启动百度if (process.Where(pro => pro.ProcessName.Contains("iexplore")).Count() == 0){System.Diagnostics.Process pro = System.Diagnostics.Process.Start("iexplore.exe", "");pro.EnableRaisingEvents = true; //启用程序退出引发事件pro.Exited += delegate(object sender1, EventArgs e1){UrlList = GetUrlListMethod();};}else {UrlList = GetUrlListMethod();}return UrlList;}/// <summary>/// 实际获取的方法,因为方法GetUrlList()中有两个方法使用到了,就做了封装/// </summary>/// <returns></returns>private List<string> GetUrlListMethod(){SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();List<string> UrlList = new List<string>();//遍历所有shellWindowforeach (SHDocVw.InternetExplorer ie in shellWindows){string filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();//测试发现有一个不是url地址,就是为了过滤它if (filename.Equals("iexplore")){UrlList.Add(ie.LocationURL.ToString());}}return UrlList;}/// <summary>/// 通过DOM操作IE(本方法模拟百度输入搜索)/// </summary>/// <param name="queryString"></param>public void OperationByDOM(string queryString) {System.Diagnostics.Process process= System.Diagnostics.Process.Start("iexplore.exe", ""); //直接打开IE浏览器,打开指定页process.EnableRaisingEvents = true; //启用程序执行完毕(或者退出)引发事件process.Exited += delegate(object sender1, EventArgs e1){//在这里编写程序执行完毕后的业务逻辑Console.WriteLine("IE浏览器打开完毕,一定要这样做");SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();SHDocVw.InternetExplorer webBrowser1 = (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count - 1);//遍历所有选项卡,看是否有百度的urlforeach (SHDocVw.InternetExplorer Browser in shellWindows){if (Browser.LocationURL.Contains("")){//获取百度的document,即拿到百度的html对象mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)Browser.Document;//查找html页面中所有的input标签mshtml.IHTMLElementCollection inputs = (mshtml.IHTMLElementCollection)doc2.all.tags("input");//获取所有input标签中id号为kw的标签,即该标签为输入框对应的idmshtml.HTMLInputElement input1 = (mshtml.HTMLInputElement)inputs.item("kw", 0);//为输入框赋值input1.value = queryString;//获取所有input标签中id号为su的标签,即为 '百度一下' 那个按钮对应的idmshtml.IHTMLElement element2 = (mshtml.IHTMLElement)inputs.item("su", 0);//模拟鼠标点击 '百度一下' 那个按钮element2.click();}}};}}}
3 主方法调用如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 操作ie浏览器{class Program{static void Main(string[] args){IEOperation operation = new IEOperation();//operation.OpenUrl("");foreach (var item in operation.GetUrlList()){Console.WriteLine(item);}operation.OperationByDOM("加菲猫");Console.Read();}}}
4 最终效果如下动图所示: