作者: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. thinkphp5使用workerman定时器定时爬取某站点新闻资讯等内容

    1.首先通过 composer 安装workerman,在thinkphp5完全开发手册的扩展->coposer包->workerman有详细说明: #在项目根目录执行以下指令compos ...

  2. ecshop 后台添加新菜单 以及 权限控制

    首先 在languages\zh_cn\admin\common.php 中添加 一级菜单 二级菜单 其次 在admin\includes\inc_menu.php 中添加 然后 在admin\inc ...

  3. Hive(6)-DML数据操作

    一. 数据导入 1. 语法 load data [local] inpath 'path' [overwrite] into table table_name [partition (partcol1 ...

  4. st link 连接问题ST LINK is not in the DFU mode plesse restart it

    原因:插上st link后做了一些操作才点击升级.如点击了连接stlink,如下图等: 解决办法: 1. 拔掉stlink 2. 插上stlink 3. 不要点其他的,直接点击ST-LINK-> ...

  5. JAVA8 Stream API的使用

    /** * @auther hhh * @date 2018/12/31 12:48 * @description Stream流:用来处理数组.集合的API * 1.不是数据结构,没有内部存储(只是 ...

  6. 第四节 Go数据结构之栈

    一.什么是栈 这是杭州地铁1号线线路图.大卫哥考考大家,地铁列车如何调头? 我也不卖关子了,列车通常是用"人字轨道"来变换车道. 列车先从A轨道开进"人字轨道" ...

  7. 从细节处谈Android冷启动优化

    本文来自网易云社区 Android APP冷启动优化,对于Android开发同学而言可能是个老生常谈的技优了. 之所以花时间写一篇冷启动优化的文章: 我想从另外一个角度来说冷启动优化,如题所述,从细节 ...

  8. Object里面的方法

    object里面有12个方法,没写完,写一些部分代表 toString():输出对象的地址字符串(hashcode码) equals():用的是==,比较的是引用,在有些类里面是重写了这个方法的,重写 ...

  9. HTMLTestRunner.py(Python3)

    """A TestRunner for use with the Python unit testing framework. Itgenerates a HTML re ...

  10. TPO-15 C1 The campus newspaper's reporter position

    TPO-15 C1 The campus newspaper's reporter position 第 1 段 1.Listen to a conversation between a Studen ...