webbrowser 常用方法(C#)
0、常用方法
- 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:获取当前正在浏览的网址的Uri
- ReadyState:获取浏览的状态
- WebBrowser控件的常用事件:
- DocumentTitleChanged,
- CanGoBackChanged,
- CanGoForwardChanged,
- DocumentTitleChanged,
- ProgressChanged,
- ProgressChanged
1、获取非input控件的值:
- webBrowser1.Document.All["控件ID"].InnerText;
- 或webBrowser1.Document.GetElementById("控件ID").InnerText;
- 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
2、获取input控件的值:
- webBrowser1.Document.All["控件ID"].GetAttribute("value");;
- 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
3、给输入框赋值:
- //输入框
- user.InnerText = "myname";
- password.InnerText = "123456";
- webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");
4、下拉、复选、多选:
- //下拉框:
- secret.SetAttribute("value", "question1");
- //复选框
- rememberme.SetAttribute("Checked", "True");
- //多选框
- cookietime.SetAttribute("checked", "checked");
5、根据已知有ID的元素操作没有ID的元素:
- HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
6、获取Div或其他元素的样式:
- webBrowser1.Document.GetElementById("addDiv").Style;
7、直接执行页面中的脚本函数,带动态参数或不带参数都行:
- Object[] objArray = new Object[1];
- objArray[0] = (Object)this.labFlightNumber.Text;
- webBrowser1.Document.InvokeScript("ticketbook", objArray);
- webBrowser1.Document.InvokeScript("return false");
8、自动点击、自动提交:
- HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
- btnAdd.InvokeMember("Click");
9、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
- 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、屏蔽脚本错误:
- 将WebBrowser控件ScriptErrorsSuppressed设置为True即可
11、自动点击弹出提示框:
- 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");//弹出提示
- }
WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)
- 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");//弹出提示
- //下面是你的执行操作代码
- }
12、获取网页中的Iframe,并设置Iframe的src
- HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;
- 或
- HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;
- docFrame.All["mainFrame"].SetAttribute("src", "http://www.baidu.com/");
13、网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
14、让控件聚焦
- this.webBrowser1.Select();
- this.webBrowser1.Focus();
- doc.All["TPL_password_1"].Focus();
15、打开本地网页文件
- webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
16、获取元素、表单
- //根据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);
- }
webbrowser 常用方法(C#)的更多相关文章
- C# Webbrowser 常用方法及多线程调用
设置控件的值 /// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagNa ...
- delphi webbrowser 常用方法示例
var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...
- WebBrowser元素定位的常用方法
在delphi中想要使用WebBrowser控件,需要一了解一些浏览器和网站制作的知识.操作元素(增删改查).需要提前了解HTML DOM.
- C#中的WebBrowser控件的使用
0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...
- 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)
本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...
- 洗礼灵魂,修炼python(68)--爬虫篇—番外篇之webbrowser模块
题外话: 爬虫学到这里,我想你大部分的网站已经不再话下了对吧?有检测报文头的,我们可以伪造报文头为浏览器,有检测IP,我们可以用代理IP,有检测请求速度的,我们可以用time模块停顿一下,需要登录验证 ...
- Winform控件学习笔记【第四天】——WebBrowser
常用方法 Navigate(string urlString);//浏览urlString表示的网址 Navigate(System.Uri url);//浏览url表示的网址 Navigate(st ...
- C# WebBrowser控件详解
作者:827969653 0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...
- c#如何判断webbrowser已经加载完毕
最近有个小程序需要采集网页源代码,但有的网页中JS脚本又会生成额外的代码,比如http://www.cnblogs.com/lidabo/p/4169396.html 红框部分便是另外加载的代码. 此 ...
随机推荐
- CentOS/RHEL Linux安装EPEL第三方软件源
https://www.vpser.net/manage/centos-rhel-linux-third-party-source-epel.html
- [MySQL] specified key was too long max key length is 767bytes
https://blog.csdn.net/u012099869/article/details/53815084/
- css3 box-sizing属性值详解
box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入widt ...
- awk进阶
整理的awk的小技巧 begin是要放在正则前面的,按照这个顺序: awk 'begin{} /.*?/ {action}end{}' file FS=':' 和 -F: 是等同的 -F 表示以 XX ...
- Nodejs JSON.parse()无法解析ObjectID和ISODate的问题
一个早上搞清楚了一个问题,关于Nodjes JSON.parse()方法只能解析字符串.布尔值.数字等,但不能解析ObjectID及ISODate的值 原因:<How to handle Obj ...
- cordova 导致css中绝对定位top:0会被顶到视图之外
IOS7+ webview全屏导致状态栏悬浮在页面上 解决方案:打开 ios项目/classes/MainViewController.m,修改viewWillAppear方法 - (void)vie ...
- linux 把ls -R格式化成树状结构
谁能写脚本把linux中的ls -R命令的结果格式化成树状结构? 最好是shell脚本!欢迎讨论! 参与讨论有可能意外获取iPhone6哦~~
- linux下安装Python3.4.1
1.下载linux 版本的 Python 我是在Windows下下载的,然后共享到linux下. 2.解压文件 tar -xvf Python-3.4.1.tar x是解压 v是查看所有过程 f是使用 ...
- 【LeetCode】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 「SCOI2016」美味
「SCOI2016」美味 题目描述 一家餐厅有 \(n\) 道菜,编号 \(1 \ldots n\) ,大家对第 \(i\) 道菜的评价值为 \(a_i \:( 1 \leq i \leq n )\) ...