LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#模拟浏览器并自动操作

admin
2021年1月29日 21:51 本文热度 2687

本文主要讲解通过WebBrowser控件打开浏览页面,并操作页面元素实现自动搜索功能,仅供学习分享使用,如有不足之处,还请指正。

涉及知识点

  1. WebBrowser:用于在WinForm窗体中,模拟浏览器,打开并导航网页。
  2. HtmlDocument:表示一个Html文档的页面。每次加载都会是一个全新的页面。
  3. GetElementById(string id):通过ID或Name获取一个Html中的元素。
  4. HtmlElement:表示一个Html标签元素。
  5. BackgroundWorker 后台执行独立操作的进程。

设计思路

主要采用异步等待的方式,等待页面加载完成,流程如下所示:


示例效果图

如下所示:加载完成后,自动输入【天安门】并点击搜索。


核心代码

加载新的页面,如下所示:

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 }


另外一种实现方式(MSHTML)

什么是MSHTML?

MSHTML是windows提供的用于操作IE浏览器的一个COM组件,该组件封装了HTML语言中的所有元素及其属性,通过其提供的标准接口,可以访问指定网页的所有元素。

涉及知识点

InternetExplorer 浏览器对象接口,其中DocumentComplete是文档加载完成事件。

HTMLDocumentClass Html文档对象类,用于获取页面元素。

IHTMLElement 获取页面元素,通过setAttribute设置属性值,和click()触发事件。

示例核心代码

如下所示:

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 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved