0、常用方法

 
  1. Navigate(string urlString):浏览urlString表示的网址
  2. Navigate(System.Uri url):浏览url表示的网址
  3. Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息
  4. //(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)
  5. GoBack():后退
  6. GoForward():前进
  7. Refresh():刷新
  8. Stop():停止
  9. GoHome():浏览主页
  10. WebBrowser控件的常用属性:
  11. Document:获取当前正在浏览的文档
  12. DocumentTitle:获取当前正在浏览的网页标题
  13. StatusText:获取当前状态栏的文本
  14. Url:获取当前正在浏览的网址的Uri
  15. ReadyState:获取浏览的状态
  16. WebBrowser控件的常用事件:
  17. DocumentTitleChanged,
  18. CanGoBackChanged,
  19. CanGoForwardChanged,
  20. DocumentTitleChanged,
  21. ProgressChanged,
  22. ProgressChanged
 

1、获取非input控件的值:

  1. webBrowser1.Document.All["控件ID"].InnerText;
  2. webBrowser1.Document.GetElementById("控件ID").InnerText;
  3. webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");

2、获取input控件的值:

  1. webBrowser1.Document.All["控件ID"].GetAttribute("value");;
  2. webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");

3、给输入框赋值:

  1. //输入框
  2. user.InnerText = "myname";
  3. password.InnerText = "123456";
  4. webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");

4、下拉、复选、多选:

 
  1. //下拉框:
  2. secret.SetAttribute("value", "question1");
  3. //复选框
  4. rememberme.SetAttribute("Checked", "True");
  5. //多选框
  6. cookietime.SetAttribute("checked", "checked");
 

5、根据已知有ID的元素操作没有ID的元素:

  1. HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;

根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。

6、获取Div或其他元素的样式:

  1. webBrowser1.Document.GetElementById("addDiv").Style;

7、直接执行页面中的脚本函数,带动态参数或不带参数都行:

  1. Object[] objArray = new Object[1];
  2. objArray[0] = (Object)this.labFlightNumber.Text;
  3. webBrowser1.Document.InvokeScript("ticketbook", objArray);
  4. webBrowser1.Document.InvokeScript("return false");

8、自动点击、自动提交:

  1. HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
  2. btnAdd.InvokeMember("Click");

9、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:

 
  1. this.timer1.Enabled = true;
  2. this.timer1.Interval = 1000 * 2;
  3. private void timer1_Tick(object sender, EventArgs e)
  4. {
  5. this.timer1.Enabled = false;
  6. ClickBtn.InvokeMember("Click");//执行按扭操作
  7. }
 

10、屏蔽脚本错误:

  1. WebBrowser控件ScriptErrorsSuppressed设置为True即可

11、自动点击弹出提示框:

 
  1. private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  2. {
  3. //自动点击弹出确认或弹出提示
  4. IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
  5. vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
  6. vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
  7. }
 

WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)

 
  1. private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  2. {
  3. //自动点击弹出确认或弹出提示
  4. IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
  5. vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
  6. vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
  7. //下面是你的执行操作代码
  8. }
 

12、获取网页中的Iframe,并设置Iframe的src

  1. HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;

  2. HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;
  3. docFrame.All["mainFrame"].SetAttribute("src", "http://www.baidu.com/");

13、网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。

14、让控件聚焦

  1. this.webBrowser1.Select();
  2. this.webBrowser1.Focus();
  3. doc.All["TPL_password_1"].Focus();

15、打开本地网页文件

  1. webBrowser1.Navigate(Application.StartupPath + @"\Test.html");

16、获取元素、表单

 
  1. //根据Name获取元素
  2. public HtmlElement GetElement_Name(WebBrowser wb,string Name)
  3. {
  4. HtmlElement e = wb.Document.All[Name];
  5. return e;
  6. }
  7.  
  8. //根据Id获取元素
  9. public HtmlElement GetElement_Id(WebBrowser wb, string id)
  10. {
  11. HtmlElement e = wb.Document.GetElementById(id);
  12. return e;
  13. }
  14.  
  15. //根据Index获取元素
  16. public HtmlElement GetElement_Index(WebBrowser wb,int index)
  17. {
  18. HtmlElement e = wb.Document.All[index];
  19. return e;
  20. }
  21.  
  22. //获取form表单名name,返回表单
  23. public HtmlElement GetElement_Form(WebBrowser wb,string form_name)
  24. {
  25. HtmlElement e = wb.Document.Forms[form_name];
  26. return e;
  27. }
  28.  
  29. //设置元素value属性的值
  30. public void Write_value(HtmlElement e,string value)
  31. {
  32. e.SetAttribute("value", value);
  33. }
  34.  
  35. //执行元素的方法,如:click,submit(需Form表单名)等
  36. public void Btn_click(HtmlElement e,string s)
  37. {
  38.  
  39. e.InvokeMember(s);
  40. }

webbrowser 常用方法(C#)的更多相关文章

  1. C# Webbrowser 常用方法及多线程调用

    设置控件的值 /// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagNa ...

  2. delphi webbrowser 常用方法示例

    var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...

  3. WebBrowser元素定位的常用方法

    在delphi中想要使用WebBrowser控件,需要一了解一些浏览器和网站制作的知识.操作元素(增删改查).需要提前了解HTML DOM.

  4. C#中的WebBrowser控件的使用

    0.常用方法   Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...

  5. 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)

    本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...

  6. 洗礼灵魂,修炼python(68)--爬虫篇—番外篇之webbrowser模块

    题外话: 爬虫学到这里,我想你大部分的网站已经不再话下了对吧?有检测报文头的,我们可以伪造报文头为浏览器,有检测IP,我们可以用代理IP,有检测请求速度的,我们可以用time模块停顿一下,需要登录验证 ...

  7. Winform控件学习笔记【第四天】——WebBrowser

    常用方法 Navigate(string urlString);//浏览urlString表示的网址 Navigate(System.Uri url);//浏览url表示的网址 Navigate(st ...

  8. C# WebBrowser控件详解

     作者:827969653     0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...

  9. c#如何判断webbrowser已经加载完毕

    最近有个小程序需要采集网页源代码,但有的网页中JS脚本又会生成额外的代码,比如http://www.cnblogs.com/lidabo/p/4169396.html 红框部分便是另外加载的代码. 此 ...

随机推荐

  1. CentOS/RHEL Linux安装EPEL第三方软件源

    https://www.vpser.net/manage/centos-rhel-linux-third-party-source-epel.html

  2. [MySQL] specified key was too long max key length is 767bytes

    https://blog.csdn.net/u012099869/article/details/53815084/

  3. css3 box-sizing属性值详解

    box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入widt ...

  4. awk进阶

    整理的awk的小技巧 begin是要放在正则前面的,按照这个顺序: awk 'begin{} /.*?/ {action}end{}' file FS=':' 和 -F: 是等同的 -F 表示以 XX ...

  5. Nodejs JSON.parse()无法解析ObjectID和ISODate的问题

    一个早上搞清楚了一个问题,关于Nodjes JSON.parse()方法只能解析字符串.布尔值.数字等,但不能解析ObjectID及ISODate的值 原因:<How to handle Obj ...

  6. cordova 导致css中绝对定位top:0会被顶到视图之外

    IOS7+ webview全屏导致状态栏悬浮在页面上 解决方案:打开 ios项目/classes/MainViewController.m,修改viewWillAppear方法 - (void)vie ...

  7. linux 把ls -R格式化成树状结构

    谁能写脚本把linux中的ls -R命令的结果格式化成树状结构? 最好是shell脚本!欢迎讨论! 参与讨论有可能意外获取iPhone6哦~~

  8. linux下安装Python3.4.1

    1.下载linux 版本的 Python 我是在Windows下下载的,然后共享到linux下. 2.解压文件 tar -xvf Python-3.4.1.tar x是解压 v是查看所有过程 f是使用 ...

  9. 【LeetCode】32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  10. 「SCOI2016」美味

    「SCOI2016」美味 题目描述 一家餐厅有 \(n\) 道菜,编号 \(1 \ldots n\) ,大家对第 \(i\) 道菜的评价值为 \(a_i \:( 1 \leq i \leq n )\) ...