C#模擬瀏覽器并自動操作
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
本文主要講解通過WebBrowser控件打開瀏覽頁面,并操作頁面元素實現(xiàn)自動搜索功能,僅供學(xué)習(xí)分享使用,如有不足之處,還請指正。 涉及知識點
設(shè)計思路主要采用異步等待的方式,等待頁面加載完成,流程如下所示: 示例效果圖如下所示:加載完成后,自動輸入【天安門】并點擊搜索。 核心代碼加載新的頁面,如下所示: 1 string url = "https://www.so.com/"; 2 this.wb01.ScriptErrorsSuppressed = true; 3 this.wb01.Navigate(url); 注意:this.wb01.ScriptErrorsSuppressed = true;用于是否彈出異常腳本代碼錯誤框 獲取元素并賦值,如下所示: 1 string search_id = "input"; 2 string search_value = "天安門"; 3 string btn_id = "search-button"; 4 HtmlDocument doc = this.wb01.Document; 5 HtmlElement search = doc.GetElementById(search_id); 6 search.SetAttribute("value", search_value); 7 HtmlElement btn = doc.GetElementById(btn_id); 8 btn.InvokeMember("click"); 示例整體代碼,如下所示: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace DemoExplorer 13 { 14 public partial class FrmExplorer : Form 15 { 16 private bool isLoadOk = false; 17 18 private BackgroundWorker bgWork; 19 20 public FrmExplorer() 21 { 22 InitializeComponent(); 23 } 24 25 private void FrmExplorer_Load(object sender, EventArgs e) 26 { 27 bgWork = new BackgroundWorker(); 28 bgWork.DoWork += bgWork_DoWork; 29 bgWork.RunWorkerCompleted += bgWork_RunWorkerCompleted; 30 string url = "https://www.so.com/"; 31 this.wb01.ScriptErrorsSuppressed = true; 32 this.wb01.Navigate(url); 33 bgWork.RunWorkerAsync(); 34 } 35 36 private void bgWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 37 { 38 string search_id = "input"; 39 string search_value = "天安門"; 40 string btn_id = "search-button"; 41 HtmlDocument doc = this.wb01.Document; 42 HtmlElement search = doc.GetElementById(search_id); 43 search.SetAttribute("value", search_value); 44 HtmlElement btn = doc.GetElementById(btn_id); 45 btn.InvokeMember("click"); 46 } 47 48 private void bgWork_DoWork(object sender, DoWorkEventArgs e) 49 { 50 compWait(); 51 } 52 53 private void compWait() 54 { 55 while (!isLoadOk) 56 { 57 Thread.Sleep(500); 58 } 59 } 60 61 private void wb01_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 62 { 63 this.wb01.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error); 64 if (this.wb01.ReadyState == WebBrowserReadyState.Complete) 65 { 66 isLoadOk = true; 67 } 68 else 69 { 70 isLoadOk = false; 71 } 72 } 73 74 private void Window_Error(object sender, HtmlElementErrorEventArgs e) 75 { 76 e.Handled = true; 77 } 78 } 79 } 另外一種實現(xiàn)方式(MSHTML) 什么是MSHTML?MSHTML是windows提供的用于操作IE瀏覽器的一個COM組件,該組件封裝了HTML語言中的所有元素及其屬性,通過其提供的標(biāo)準(zhǔn)接口,可以訪問指定網(wǎng)頁的所有元素。 涉及知識點InternetExplorer 瀏覽器對象接口,其中DocumentComplete是文檔加載完成事件。 HTMLDocumentClass Html文檔對象類,用于獲取頁面元素。 IHTMLElement 獲取頁面元素,通過setAttribute設(shè)置屬性值,和click()觸發(fā)事件。 示例核心代碼如下所示: 1 namespace AutoGas 2 { 3 public class Program 4 { 5 private static bool isLoad = false; 6 7 public static void Main(string[] args) 8 { 9 string logUrl = ConfigurationManager.AppSettings["logUrl"]; //登錄Url 10 string uid = ConfigurationManager.AppSettings["uid"];//用戶名ID 11 string pid = ConfigurationManager.AppSettings["pid"];//密碼ID 12 string btnid = ConfigurationManager.AppSettings["btnid"];//按鈕ID 13 string uvalue = ConfigurationManager.AppSettings["uvalue"];//用戶名 14 string pvalue = ConfigurationManager.AppSettings["pvalue"];//密碼 15 InternetExplorer ie = new InternetExplorerClass(); 16 ie.DocumentComplete += Ie_DocumentComplete; 17 object c = null; 18 ie.Visible = true; 19 ie.Navigate(logUrl, ref c, ref c, ref c, ref c); 20 ie.FullScreen = true; 21 22 compWait(); 23 try 24 { 25 HTMLDocumentClass doc = (HTMLDocumentClass)ie.Document; 26 IHTMLElement username = doc.getElementById(uid); 27 IHTMLElement password = doc.getElementById(pid); 28 IHTMLElement btn = doc.getElementById(btnid); 29 //如果有session,則自動登錄,不需要輸入賬號密碼 30 if (username != null && password != null && btn != null) 31 { 32 username.setAttribute("value", uvalue); 33 password.setAttribute("value", pvalue); 34 btn.click(); 35 } 36 } 37 catch (Exception ex) { 38 39 } 40 } 41 42 public static void compWait() { 43 while (!isLoad) 44 { 45 Thread.Sleep(200); 46 } 47 } 48 49 /// <summary> 50 /// 51 /// </summary> 52 /// <param name="pDisp"></param> 53 /// <param name="URL"></param> 54 private static void Ie_DocumentComplete(object pDisp, ref object URL) 55 { 56 isLoad = true; 57 } 58 } 59 } 備注所謂的堅持,不過是每天努力一點點?。?! 該文章在 2021/1/29 21:51:18 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |