C#模拟浏览器实现自动操作
作者:无熵~
这篇文章主要为大家详细介绍了如何使用C#实现模拟浏览器实现自动操作,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
使用WebBrowser加载网页(内嵌)
通过WebBrowser控件打开浏览页面,并操作页面元素实现自动搜索功能
//是否已经加载网页标准 private bool isLoadok = false; //首次加载网页标准 private bool firstLoaded = false; //后台服务 private BackgroundWorker bgwork; public WebBrowserDemo() { InitializeComponent(); } /// <summary> /// 自动窗体后加载网页并获取网络元素,实现赋值,点击操作 /// winform窗体上添加了一个名为webBrowser1的WebBrowser控件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { this.BeginInvoke(new Action(() => { bgwork = new BackgroundWorker(); bgwork.DoWork += Bgwork_DoWork; bgwork.RunWorkerCompleted += Bgwork_RunWorkerCompleted; string url = "https://www.so.com/"; this.webBrowser1.ScriptErrorsSuppressed = true; this.webBrowser1.Navigate(url); webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted; bgwork.RunWorkerAsync(); 判断网页是否加载完成:等待不起作用,一直在加载网页,使用DocumentCompleted事件 //while (this.webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete) //{ // Thread.Sleep(1000); //} //string search_id = "input"; //string search_value = "天安门"; //string btn_id = "search-button"; //HtmlDocument doc = this.webBrowser1.Document; //HtmlElement search = doc.GetElementById(search_id); //search.SetAttribute("value", search_value); //HtmlElement btn = doc.GetElementById(btn_id); //btn.InvokeMember("click"); })); } /// <summary> /// WebBrowser加载网页完成事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { this.BeginInvoke(new Action(() => { if (this.webBrowser1.IsBusy == false && webBrowser1.ReadyState == WebBrowserReadyState.Complete&& firstLoaded==false) { firstLoaded=true; string search_id = "input"; string search_value = "自动化"; string btn_id = "search-button"; HtmlDocument doc = this.webBrowser1.Document; HtmlElement search = doc.GetElementById(search_id); search.SetAttribute("value", search_value); HtmlElement btn = doc.GetElementById(btn_id); btn.InvokeMember("click"); } })); } /// <summary> /// 后台服务完成事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Bgwork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.webBrowser1.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error); if(webBrowser1.ReadyState == WebBrowserReadyState.Complete) { isLoadok = true; } else { isLoadok = false; } } /// <summary> /// 加载网页错误事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Error(object sender, HtmlElementErrorEventArgs e) { e.Handled = true; } /// <summary> /// 后台服务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Bgwork_DoWork(object sender, DoWorkEventArgs e) { compWait(); } /// <summary> /// 后台服务方法 /// </summary> private void compWait() { while(!isLoadok) { Thread.Sleep(500); } }
使用 Selenium WebDriver加载网页(独立)
Selenium WebDriver 是一个基于 Web 的自动化测试框架,可以测试在各种Web浏览器和各种操作系统上启动的网页。可以自由使用各种编程语言(例如Java、Perl、Python、Ruby、C#、PHP 和 JavaScript)编写测试脚本。注:可以不需要下载webdriver驱动
private void btnLoad_Click(object sender, EventArgs e) { // 设置Chrome选项,可选 var options = new ChromeOptions(); //options.AddArgument("--headless"); // 无头模式,不打开浏览器窗口 // 初始化WebDriver IWebDriver driver = new ChromeDriver(options); try { // 打开网页 driver.Navigate().GoToUrl("https://account.chsi.com.cn/passport/login"); //driver.Navigate().Back(); //driver.Navigate().Refresh(); // 等待页面加载完成,知道页面空闲,超时10s抛出异常 //var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); //wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.BrowserIsNotBusy()); //查找用户输入框并输入用户名 driver.FindElement(By.Name("username")).SendKeys("Tom"); //查找密码输入框并输入密码 driver.FindElement(By.Name("password")).SendKeys("123456"); driver.Manage().Window.Maximize(); 查找提交按钮并点击提交按钮 //driver.FindElement(By.ClassName("btn_login")).Click(); // 获取页面源码 string pageSource = driver.PageSource; Console.WriteLine(pageSource); } catch(Exception ex) { Console.WriteLine(ex.Message); } finally { 清理,关闭浏览器 //driver.Quit(); } }
到此这篇关于C#模拟浏览器实现自动操作的文章就介绍到这了,更多相关C#浏览器自动操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!