常用方法
|
1
2
3
4
5
6
7
8 |
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();//浏览主页 |
常用属性
|
1
2
3
4
5 |
Document;//获取当前正在浏览的文档DocumentTitle;//获取当前正在浏览的网页标题StatusText;//获取当前状态栏的文本Url;//获取当前正在浏览的网址的UriReadyState;//获取浏览的状态 |
常用事件
|
1 |
DocumentCompleted//承载的文档全部加载后发生 |
获取控件的值
|
1
2
3
4
5
6 |
//input取值webBrowser1.Document.All["控件ID"].GetAttribute("value");webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");//非input取值webBrowser1.Document.All["控件ID"].InnerText;webBrowser1.Document.GetElementById("控件ID").InnerText; |
给输入框赋值
|
1
2
3
4
5
6
7
8
9
10 |
HtmlElement he = webBrowser1.Document.GetElementById("控件ID");//input赋值he.InnerText = "内容";he.SetAttribute("value", "值");//下拉框赋值he.SetAttribute("value", "question1");//复选框he.SetAttribute("Checked", "True");//多选框he.SetAttribute("checked", "checked"); |
网页中的元素包含:属性、属性的值、html标记、文本。
HtmlElement对象:
GetAttribute("value");//获取属性value的值
SetAttribute("value", "123");//设置属性value的值为123
InnerText;//获取元素下的文本
InnerHtml;//获取元素下的html标记
操作元素的子元素或父元素
|
1 |
HtmlElement he = webBrowser1.Document.GetElementById(passengerId).Parent.FirstChild.Children[1]; |
Parent;//获取元素的父元素
FirstChild;//获取元素的子元素
Children;//获取元素的子元素的集合
获取元素样式
|
1 |
webBrowser1.Document.GetElementById("元素ID").Style; |
自动提交、自动点击
|
1 |
webBrowser1.Document.GetElementById("元素ID").InvokeMember("Click"); |
屏蔽脚本错误
|
1 |
webBrowser1.ScriptErrorsSuppressed = true; |
控件聚焦
|
1
2
3 |
webBrowser1.Select();webBrowser1.Focus();webBrowser1.Document.GetElementById("元素ID").Focus(); |
设置编辑模式
|
1
2
3
4
5
6
7 |
webBrowser1.DocumentText=string.Empty;webBrowser1.Document.ExecCommand("EditMode",false,null);webBrowser1.Document.ExecCommand("LiveResize",false,null);//编辑模式下后台写入内容webBrowser1.Document.Write(@"现在");//如果在编辑模式下这样设置的话就不能再编辑了webBrowser1.DocumentText=@"现在"; |
获取主体内容
|
1
2 |
webBrowser1.Document.Body.InnerText;webBrowser1.Document.Body.InnerHtml; |
执行页面脚本
|
1
2
3
4 |
Object[] objArray = new
Object[1];objArray[0] = (Object)"webbrowser";webBrowser1.Document.InvokeScript("runabc", objArray);webBrowser1.Document.InvokeScript("return false") |
获取网页中的Iframe
|
1
2 |
HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document; |
打开新链接的处理方法
处理新打开的链接
|
1
2
3
4
5
6 |
private
void
webBrowser1_NewWindow(object
sender, CancelEventArgs e){ Uri a = new
Uri(webBrowser1.Document.ActiveElement.GetAttribute("href")); webBrowser1.Navigate(a); e.Cancel = true;//取消在默认浏览器中打开} |
WebBrowser脚本错误的解决方案
这种方法不能彻底解决,可以部分解决问题。
|
1
2
3
4
5
6
7
8 |
//捕获控件的错误this.WebBrowser.Document.Window.Error += new
HtmlElementErrorEventHandler(Window_Error);//对错误进行处理void
Window_Error(object
sender, HtmlElementErrorEventArgs e){ // 自己的处理代码 e.Handled = true;} |
上面的方法对于多个框架嵌套等等的情形还是不能很好的解决。
附录:
|
属性 |
说明 |
|
Application |
如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDispatch)。如果在宿主对象中自动化对象无效,这个程序将返回WebBrowser |
|
Parent |
返回WebBrowser控件的父自动化对象,通常是一个容器,例如是宿主或IE窗口 |
|
Container |
返回WebBrowser控件容器的自动化对象。通常该值与Parent属性返回的值相同 |
|
Document |
为活动的文档返回自动化对象。如果HTML当前正被显示在WebBrowser中,则 |
|
TopLevelContainer |
返回一个Boolean值,表明IE是否是WebBrowser控件顶层容器,是就返回true |
|
Type |
返回已被WebBrowser控件加载的对象的类型。例如:如果加载.doc文件,就会返 |
|
Left |
返回或设置WebBrowser控件窗口的内部左边与容器窗口左边的距离 |
|
Top |
返回或设置WebBrowser控件窗口的内部左边与容器窗口顶边的距离 |
|
Width |
返回或设置WebBrowser窗口的宽度,以像素为单位 |
|
Height |
返回或设置WebBrowser窗口的高度,以像素为单位 |
|
LocationName |
返回一个字符串,该字符串包含着WebBrowser当前显示的资源的名称,如果资源 |
|
LocationURL |
返回WebBrowser当前正在显示的资源的URL |
|
Busy |
返回一个Boolean值,说明WebBrowser当前是否正在加载URL,如果返回true |
事件:
|
PrivateEvents |
Description |
|
BeforeNavigate2 |
导航发生前激发,刷新时不激发 |
|
CommandStateChange |
当命令的激活状态改变时激发。它表明何时激活或关闭Back和Forward |
|
DocumentComplete |
当整个文档完成是激发,刷新页面不激发 |
|
DownloadBegin |
当某项下载操作已经开始后激发,刷新也可激发此事件 |
|
DownloadComplete |
当某项下载操作已经完成后激发,刷新也可激发此事件 |
|
NavigateComplete2 |
导航完成后激发,刷新时不激发 |
|
NewWindow2 |
在创建新窗口以前激发 |
|
OnFullScreen |
当FullScreen属性改变时激发。该事件采用VARIENT_BOOL的一个输 |
|
OnMenuBar |
改变MenuBar的属性时激发,标示参数是VARIENT_BOOL类型的。 |
|
OnQuit |
无论是用户关闭浏览器还是开发者调用Quit方法,当IE退出时就会激发 |
|
OnStatusBar |
与OnMenuBar调用方法相同,标示状态栏是否可见。 |
|
OnToolBar |
调用方法同上,标示工具栏是否可见。 |
|
OnVisible |
控制窗口的可见或隐藏,也使用一个VARIENT_BOOL类型的参数 |
|
StatusTextChange |
如果要改变状态栏中的文字,这个事件就会被激发,但它并不理会程序是否有状态栏 |
|
TitleChange |
Title有效或改变时激发 |
参考: C# WinForm开发系列 - WebBrowser - peterzb - 博客园
Winform控件学习笔记【第四天】——WebBrowser
原文:http://www.cnblogs.com/zxlovenet/p/3527687.html