本文转自 http://www.open-open.com/code/view/1430559996802

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);

}

009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)的更多相关文章

  1. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. 在VC中使用WebBrowser控件的两方法

    ClassWizard方式: 1.创建包装类:View->ClassWizard->Add Class->Form a Type Library->C:/winnt/syste ...

  3. C#中实现WebBrowser控件的HTML源代码读写

    原文:C#中实现WebBrowser控件的HTML源代码读写 C#中实现WebBrowser控件的HTML源代码读写http://www.blogcn.com/user8/flier_lu/index ...

  4. vs2005中的WebBrowser控件的简单应用

    原文:vs2005中的WebBrowser控件的简单应用 这个控件被封装了一下,和以前的调用方式稍有不同.事件还是那几个,变化不大.方法变了不少.从网上能查到的资料不多,贴出一些代码来作参考.看看这段 ...

  5. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

  6. django django中的HTML控件及参数传递方法 以及 HTML form 里的数据是怎么被包成http request 的?如何在浏览器里查看到这些数据?

    https://www.jb51.net/article/136738.htm django中的HTML控件及参数传递方法 下面小编就为大家分享一篇django中的HTML控件及参数传递方法,具有很好 ...

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

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

  8. WPF中禁止WebBrowser控件打开新窗口

    一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...

  9. C#中的WebBrowser控件加载ActiveX插件

    C#中WebBrowser控件,将项目输入更改为x86输出,在页面打开时即可自动加载ActiveX控件

随机推荐

  1. OpenSesame:一个能够攻击fixed-pin设备的工具

    OpenSesame是一种设备,这种设备可以通过无线技术来打开任何一个设有固定密码的车库门,我从中发现了一个攻击无线固定pin码设备的新方法. 演示视频以及详细信息: opensesame源代码:ht ...

  2. Alice and Bob(不断补充)

    我之前做过一些博弈的题目,以为博弈都是DP,结果被坑了很多次,其实博弈有很多种,在此,把我见过的类型都搬上来. 1,HDU3951(找规律) 题意:把n枚硬币围成一个圆,让Alice和Bob两个人分别 ...

  3. Euro Efficiency_完全背包

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  4. Palindrome_滚动数组&&DP

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  5. Java课程作业1

    模仿JavaAppArguments.java实例,编写一个程序,此程序从命令行接受多个数字,求和之后输出. 设计思想:命令行参数都是字符串,必须将其转化成数字才能相加,定义一个数组接收字符串转化的数 ...

  6. 实现Magento多文件上传代码功能开发

    在Magento中上传单个文件很简单,可以直接在继承的Mage_Adminhtml_Block_Widget_Form类中直接添加如下组件Field:  对于图片:   $fieldset->a ...

  7. iOS-微信支付平台

    微信支付1:去微信开放平台注册应用https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN&token=2c0f ...

  8. 让UIWebView弹出键盘上的按钮显示中文

    UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的, ...

  9. Git 基础学习篇(应用-windows篇)

    此篇教程主要是讲应用,因为理论,,,额,我也说不出来.大家要深入学习还是看廖老师的教程吧. 可以把这篇当作一个简单应用的参考,因为当初看廖老师的也难看啊!!! 以下是资料: 廖雪峰-Git教程 [Gi ...

  10. How to use HaploView

    HaploView is a program that is used to visualize the LD blocks of  SNPs. What I need to do is the fo ...