作者:827969653
 
 
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 = "";
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[].FirstChild.FirstChild;
根据Parent,FirstChild,Children[]数组,多少层级的元素都能找到。 6、获取Div或其他元素的样式: webBrowser1.Document.GetElementById("addDiv").Style; 7、直接执行页面中的脚本函数,带动态参数或不带参数都行: Object[] objArray = new Object[];
objArray[] = (Object)this.labFlightNumber.Text;
webBrowser1.Document.InvokeScript("ticketbook", objArray);
webBrowser1.Document.InvokeScript("return false"); 8、自动点击、自动提交: HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
btnAdd.InvokeMember("Click"); 、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
this.timer1.Enabled = true;
this.timer1.Interval = * ;
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/"); 、网页中存在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);
}
 
 

17、禁用超链接
超链接分为两种,一种是 当前窗口直接转向, 一种是 在新窗口中打开

当然窗口直接转向:
将 WebBrowser 的 AllowNavigation 设为 false

在新窗口中打开:
禁用新窗口打开,需要处理 WebBrowser 的 NewWindow 事件

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
     e.Cancel = true;
}

18、禁用错误脚本提示
将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true

19、禁用右键菜单
将 WebBrowser 的 IsWebBrowserContextMenuEnabled 设为 false

20、禁用快捷键
将 WebBrowser 的 WebBrowserShortcutsEnabled 设为 false

最后注意:
将WebBrowser 的 AllowWebBrowserDrop 设为 false,不要忘记了哦。

C# WebBrowser控件详解的更多相关文章

  1. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  2. picker控件详解与使用,(实现省市的二级联动)

    picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...

  3. Switch控件详解

    Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...

  4. ToolBar控件详解

    ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...

  5. Spinner控件详解

    Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...

  6. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  7. Android开发:文本控件详解——TextView(二)文字跑马灯效果实现

    一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...

  8. C++ CComboBox控件详解

    转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...

  9. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

随机推荐

  1. php无限级分类----封装函数

    public function catetree($cateRes){//传递过来的数据资源 return $this->sort($cateRes); 调用函数 } public functi ...

  2. php 计算两个文件的相对路径

    <?php /** * 计算两个文件的相对路径 */ function relative_path($path1, $path2) { $arr1 = explode('/', dirname( ...

  3. pyhton实现简单的木马程序

    十一的晚上,平时都在写工作的代码,好久没有专门看一些知识了,感觉想刚开始学c一样,搞到半夜 还是<python网络编程基础>,写了小脚本,没有任何结构,一句一句的往下写的,反正是可以实现想 ...

  4. Linux常用97条命令

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a  列出包括.a开头的隐藏文件的所有文件 -A  通-a,但不列出"."和".." -l  ...

  5. Ubuntu中 MySQL 的中文编码问题

    使用Ubuntu在安装好MySQL数据库之后,如果直接创建数据库,再创建数据表,那么是无法向字段插入中文的,会报Incorrect string value错误. c实现编码设置的两种方法: (1)动 ...

  6. 基于Python的飞机大战游戏

    前几天决定学Python,上网找了教程看了两天,和C比起来面向对象的特性真的都很便捷,有了类开发各种敌机,子弹什么的都很方便. 在此要感谢开发pygame模块的开发人员,真的很好用(逃 效果图↓ 主函 ...

  7. centos7关闭图形界面启动系统

    手动敲那么多不累么?仅2条命令(好) 1,命令模式systemctl set-default multi-user.target 2,图形模式systemctl set-default graphic ...

  8. 深圳Uber司机本周(7.13-7.19凌晨4:00)的奖励政策

    本周(7.13-7.19凌晨4:00)的奖励政策为: 佣金返还: 车费的20%适用于所有产品(不包括Tesla)无获取条件 翻倍补贴: 每周一到周四07:00-10:00/17:00-22:00:车费 ...

  9. [并发并行]_[线程模型]_[Pthread线程使用模型之一管道Pipeline]

    场景 1.经常在Windows, MacOSX 开发C多线程程序的时候, 经常需要和线程打交道, 如果开发人员的数量不多时, 同时掌握Win32和pthread线程 并不是容易的事情, 而且使用Win ...

  10. 【LG3527】[POI2011]MET-Meteors

    [LG3527][POI2011]MET-Meteors 题面 洛谷 题解 整体二分. 每次二分\(mid\),如果到时间\(mid\)以收集过\(P_i\)就存入子序列\(L\),否则存入子序列\( ...