webBrowser(网络转载)
C#WebBrowser控件使用教程与技巧收集--苏飞收集
先来看看常用的方法
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
Navigate(string urlString):浏览urlString表示的网址Navigate(System.Uri url):浏览url表示的网址Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息//(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)GoBack():后退GoForward():前进Refresh():刷新Stop():停止GoHome():浏览主页WebBrowser控件的常用属性:Document:获取当前正在浏览的文档DocumentTitle:获取当前正在浏览的网页标题StatusText:获取当前状态栏的文本Url:获取当前正在浏览的网址的UriReadyState:获取浏览的状态WebBrowser控件的常用事件:DocumentTitleChanged,CanGoBackChanged,CanGoForwardChanged,DocumentTitleChanged,ProgressChanged,ProgressChangedDocumentCompleted 页面加载完成之后的事件复制代码 |
1、获取非input控件的值:
|
01
02
03
|
webBrowser1.Document.All["控件ID"].InnerText;或webBrowser1.Document.GetElementById("控件ID").InnerText;或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value"); |
2.获取input控件的值:
|
01
02
|
webBrowser1.Document.All["控件ID"].GetAttribute("value");;或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value"); |
3、给输入框赋值:
|
01
02
03
04
|
//输入框user.InnerText = "myname";password.InnerText = "123456";webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123"); |
4、下拉、复选、多选:
|
01
02
03
04
05
06
|
//下拉框:secret.SetAttribute("value", "question1"); //复选框rememberme.SetAttribute("Checked", "True");//多选框cookietime.SetAttribute("checked", "checked"); |
5、根据已知有ID的元素操作没有ID的元素:
|
01
|
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild; |
根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
6、获取Div或其他元素的样式:
|
01
|
webBrowser1.Document.GetElementById("addDiv").Style; |
7、直接执行页面中的脚本函数,带动态参数或不带参数都行:
|
01
02
03
04
|
Object[] objArray = new Object[1];objArray[0] = (Object)this.labFlightNumber.Text;webBrowser1.Document.InvokeScript("ticketbook", objArray);webBrowser1.Document.InvokeScript("return false"); |
8、自动点击、自动提交:
|
01
02
|
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;btnAdd.InvokeMember("Click"); |
9、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
|
01
02
03
04
05
06
07
|
this.timer1.Enabled = true;this.timer1.Interval = 1000 * 2;private void timer1_Tick(object sender, EventArgs e){ this.timer1.Enabled = false; ClickBtn.InvokeMember("Click");//执行按扭操作} |
10、屏蔽脚本错误:
|
01
|
将WebBrowser控件ScriptErrorsSuppressed设置为True即可 |
11、自动点击弹出提示框:
|
01
02
03
04
05
06
07
|
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e){ //自动点击弹出确认或弹出提示 IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认 vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示} |
12.WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)
|
01
02
03
04
05
06
07
08
|
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){ //自动点击弹出确认或弹出提示 IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认 vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示 //下面是你的执行操作代码} |
13、获取网页中的Iframe,并设置Iframe的src
|
01
02
03
04
|
HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document; 或HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document; docFrame.All["mainFrame"].SetAttribute("src", "http://www.sufeinet.com/"); |
网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
14、让控件聚焦
|
01
02
03
|
this.webBrowser1.Select();this.webBrowser1.Focus();doc.All["TPL_password_1"].Focus(); |
15、打开本地网页文件
|
01
|
webBrowser1.Navigate(Application.StartupPath + @"\Test.html"); |
16、获取元素、表单
|
01
02
03
04
05
06
07
08
09
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
|
//根据Name获取元素public HtmlElement GetElement_Name(WebBrowser wb,string Name){ HtmlElement e = wb.Document.All[Name]; return e;}//根据Id获取元素public HtmlElement GetElement_Id(WebBrowser wb, string id){ HtmlElement e = wb.Document.GetElementById(id); return e;}//根据Index获取元素public HtmlElement GetElement_Index(WebBrowser wb,int index){ HtmlElement e = wb.Document.All[index]; return e;}//获取form表单名name,返回表单public HtmlElement GetElement_Form(WebBrowser wb,string form_name){ HtmlElement e = wb.Document.Forms[form_name]; return e;}//设置元素value属性的值public void Write_value(HtmlElement e,string value){ e.SetAttribute("value", value);}//执行元素的方法,如:click,submit(需Form表单名)等public void Btn_click(HtmlElement e,string s){ e.InvokeMember(s);} |
17。获取Cookie
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool InternetGetCookieEx(string pchUrl, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved); private static string GetCookieString(string url) { uint datasize = 1024; StringBuilder cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero)) { if (datasize < 0) return null; cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero)) return null; } return cookieData.ToString(); } private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e) { richTextBox1.Text = string.Empty; if (cbcookie.Checked) { if (checkBox1.Checked) { richTextBox1.Text = GetCookieString(textBox1.Text.Trim()); } else { richTextBox1.Text = webBrowser1.Document.Cookie; } } } |
18.怎么设置代理
http://www.sufeinet.com/thread-2242-1-1.html
19.怎么在加载完成某个页面之后执行代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
//本事件是当每次加载完成当前页面后才会执行的 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //e.Url是当前加载的页面, if (e.Url.ToString().Contains("http://sufeinet.com")) { //执行操作1 } else if (e.Url.ToString().Contains("http://baidu.com")) { //执行操作2 } } |
20.怎么禁止在新窗口中打开网页
|
01
02
03
04
05
06
07
|
private void webBrowser1_NewWindow(object sender, CancelEventArgs e){ string url = ((System.Windows.Forms.WebBrowser)sender).StatusText; webBrowser1.Navigate(url); e.Cancel = true; } |
21.怎么设置Cookie
|
01
|
webBrowser1.Document.Cookie=“你的Cookie值”; |
webBrowser(网络转载)的更多相关文章
- 8张图带你理解Java整个只是网络(转载)
8张图带你理解Java整个只是网络 一图胜千言,下面图解均来自Program Creek 网站的Java教程,目前它们拥有最多的票选.如果图解没有阐明问题,那么你可以借助它的标题来一窥究竟. 1.字符 ...
- [matlab] 18.图与网络 (转载)
基本概念: 图论[Graph Theory]是数学的一个分支.它以图为研究对象.图论中的图是由若干给定的点及连接两点的线所构成的图形,这种图形通常用来描述某些事物之间的某种特定关系,用点代表事物,用连 ...
- 用linux命令连接无线网络-转载
首先是用到的工具: ifconfigrouteiwlistiwconfig 后两个是无线工具 从现在开始,按我的步骤做 (##后面的是说明部分) 1.开启无线,如果是笔记本,开启无线开关,或用Fn+F ...
- 网络转载:局域网安全:解决ARP攻击的方法和原理
局域网安全:解决ARP攻击的方法和原理 IT世界网2006-01-26 10:17 [故障原因] 局域网内有人使用ARP欺骗的木马程序(比如:传奇盗号的软件,某些传奇外挂中也被恶意加载了此程序). ...
- SDN(Software Defined Network):软件定义网络----转载
SDN(Software Defined Network):软件定义网络 传统的网络转发行为: 1)逐设备单独控制,纯分布式控制. 2)控制面和转发面在同一个设备中,耦合紧密. 管理员无法直接操控转发 ...
- C# Redis Server分布式缓存编程 --网络转载
这篇文章我将介绍如果用最简洁的方式配置Redis Server, 以及如何使用C#和它交互编程 一. 背景介绍 Redis是最快的key-value分布式缓存之一 缺点: 没有本地数据缓冲, 目前还没 ...
- 网络转载——java接口的概念
为什么会出现接口? 接口的出现是为了扩展java中的类继承的单调性.这样使得功能更加丰富. 接口关键字? 定义接口interface,实现一个接口 implements 什么接口呢? 接口是一种特殊 ...
- Makefile <网络转载>
陈皓 (CSDN)概述——什 么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的 ...
- JavaScript中JSONObject和JSONArray相关知识备忘(网络转载)
1.json的格式,有两种: {"key": "value"} //JSONObject(对象) [{"key1": "value ...
随机推荐
- HDU 3829 Cat VS Dog(最大独立集)
题目大意: 有n只猫,有m只狗.现在有P个学生去参观动物园.每个孩子有喜欢的动物和不喜欢的动物.假如他喜欢猫那么他就一定不喜欢狗(反之亦然). 如果一个孩子喜欢一个动物,那么这个动物不会被移除,若是不 ...
- 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...
- 在kafka上对topic新增partition
对topic增加partition 参考官网site:http://kafka.apache.org/documentation.html#basic_ops_modify_topic 通过kafka ...
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- 关于STM32 定时器 PWM 实时调节占空比时,预装载特性
最近在调试项目的时候遇到一个奇怪的现象:在调试状态下,给定时器捕获比较寄存器赋不同值,能产生不同占空比的波形(图1).反映到器件上也有不同的电压显示,但是在设备运行的时候,就不行了(图2). 图1 图 ...
- 嵌套fragment时必须要重写 onDetach()
/** * 嵌套fragment时必须要重写 onDetach()如下 */ @Override public void onDetach() { super.onDetach(); ...
- linux0.12 解决编译问题常用命令
解决编译问题时,经常需要修改所有的Makefile,特别定义了下面几条命令方便修改. function msed() { find -name "Makefile" -exec s ...
- Java中this的功能与作用
粗粒度上来说,Java中关键字this主要有2个功能: 1.表示“当前对象”的引用 (1)作为参数传入 [程序实例1] public class MyObject { public Integer v ...
- hdu4010 Query On The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- Event 元标签中的代码提示问题
自定的事件可以利用Event元标签在支持该事件的类里面做绑定,绑定后FlashBuilder会有代码提示,以提示该类支持的事件类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...