C#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
可以实现例如通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;可以自动点击网页上的按钮等功能
1. 加入对Microsoft Internet Controls的引用;
2. 加入对Microsoft HTML Object Library的引用;
(要引入Microsoft.mshtml.dll 地址是C:/Program Files/Microsoft.NET/Primary Interop Assemblies)
3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;
4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;
5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);
6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;
简单来说:
打开ie:
1 2 3 4 5 6 7 | 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 c,ref c,ref c,ref c); )
mshtml.IHTMLDocument2 htmlDoc = webBrowser1.Document as mshtml.IHTMLDocument2;
//...获取WebBroswer中的body代码
1 2 3 | mshtml.HTMLDocumentClass doc=(mshtml.HTMLDocumentClass)myWeb.Document; mshtml.HTMLBody body=(mshtml.HTMLBody)docCC.body; string html=body.innerHTML.ToString(); |
//...如果里面有Form,要给里面的text填充信息
1 2 3 4 5 6 | 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= "填充信息" ; |
//...要点击里面的某个按钮
1 2 3 4 5 | 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属性的值:
1 2 3 | mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser1.Document; mshtml.IHTMLElement img = (mshtml.IHTMLElement)doc2.all.item( "regimg" , 0); string imgUrl = ( string )img.getAttribute( "src" ); |
2、填写表单,并确定
1 2 3 4 5 6 7 8 | mshtml.IHTMLElement loginname = (mshtml.IHTMLElement)doc2.all.item( "loginname" , 0); mshtml.IHTMLElement loginPW = (mshtml.IHTMLElement)doc2.all.item( "password" , 0); mshtml.IHTMLElement loginBT = (mshtml.IHTMLElement)doc2.all.item( "formsubmit" , 0); mshtml.IHTMLElement loginYZ = (mshtml.IHTMLElement)doc2.all.item( "regcode" , 0); loginname.setAttribute( "value" , tbLoginName.Text); loginPW.setAttribute( "value" , tbLoginPassWord.Password); loginYZ.setAttribute( "value" , tbYZ.Text); loginBT.click(); |
3、获取源码
1 | textBox1.Text = doc2.body.innerHTML; |
4、执行JS
1 2 | mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow; win.execScript( "changeRegImg()" , "javascript" ); //使用JS |
5、禁止JS,WPF下目前发现唯一适用的一种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 ); } |
下面是另外一遍博客里写的比较好的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #region Search public static void Search( string searchText) { SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle(); mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle( ref ieWnd); System.Diagnostics.Trace.Assert(ieDoc != null ); SearchTextInGoogle(ieDoc, searchText); //activate ie window SetForegroundWindow(ieWnd.HWND); } #endregion #region get ie window of google page public static SHDocVw.InternetExplorer GetIEWndOfGoogle() { mshtml.IHTMLDocument2 ieDoc; SHDocVw.InternetExplorer ieWnd = null ; SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer ie in shellWindows) { //if it is ie window if (ie.FullName.ToUpper().IndexOf( "IEXPLORE.EXE" ) > 0) { //get the document displayed ieDoc = (mshtml.IHTMLDocument2)ie.Document; if (ieDoc.title.ToUpper().IndexOf( "GOOGLE" ) >= 0) { ieWnd = ie; break ; } } } shellWindows = null ; return ieWnd; } #endregion #region get ie document of google page public static mshtml.IHTMLDocument2 GetIEDocOfGoogle( ref SHDocVw.InternetExplorer ieWnd) { object missing = null ; mshtml.IHTMLDocument2 ieDoc; if (ieWnd == null ) { ieWnd = new SHDocVw.InternetExplorer(); ieWnd.Visible = true ; ieWnd.Navigate( "http://www.google.com" , ref missing, ref missing, ref missing, ref missing); //wait for loading completed, or using DocumentComplete Event while (ieWnd.StatusText.IndexOf( "完成" ) == -1) Application.DoEvents(); } ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document; return ieDoc; } #endregion #region Search the given text in google ///// /// search the given text in google home page /// we can see the source file of google home page to confirm the elements we need /// the html file of google home page is as follows /// ... ///// public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText) { mshtml.HTMLInputElementClass input; //set the text to be searched foreach (mshtml.IHTMLElement ieElement in ieDoc.all) { //if its tag is input and name is q(question) if (ieElement.tagName.ToUpper().Equals( "INPUT" )) { input = ((mshtml.HTMLInputElementClass)ieElement); if (input.name == "q" ) { input.value = searchText; break ; } } } //click the submit button to search foreach (mshtml.IHTMLElement ieElement in ieDoc.all) { //if its tag is input if (ieElement.tagName.ToUpper().Equals( "INPUT" )) { input = (mshtml.HTMLInputElementClass)ieElement; if (input.name == "btnG" ) { input.click(); break ; } } } } |
#endregion
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!