C#中的WebBrowser控件的使用
關(guān)鍵字:C# WebBrowser
作者:txw1958
原文:http://www.cnblogs.com/txw1958/archive/2012/09/24/CSharp-WebBrowser.html
?
0、常用方法
Navigate(string urlString):瀏覽urlString表示的網(wǎng)址 Navigate(System.Uri url):瀏覽url表示的網(wǎng)址 Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 瀏覽urlString表示的網(wǎng)址,并發(fā)送postData中的消息 //(通常我們登錄一個(gè)網(wǎng)站的時(shí)候就會(huì)把用戶名和密碼作為postData發(fā)送出去) GoBack():后退 GoForward():前進(jìn) Refresh():刷新 Stop():停止 GoHome():瀏覽主頁(yè) WebBrowser控件的常用屬性: Document:獲取當(dāng)前正在瀏覽的文檔 DocumentTitle:獲取當(dāng)前正在瀏覽的網(wǎng)頁(yè)標(biāo)題 StatusText:獲取當(dāng)前狀態(tài)欄的文本 Url:獲取當(dāng)前正在瀏覽的網(wǎng)址的Uri ReadyState:獲取瀏覽的狀態(tài) 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控件的值:
?
3、給輸入框賦值:
//輸入框 user.InnerText = "myname"; password.InnerText = "123456"; webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");?
4、下拉、復(fù)選、多選:
//下拉框: secret.SetAttribute("value", "question1"); //復(fù)選框 rememberme.SetAttribute("Checked", "True"); //多選框 cookietime.SetAttribute("checked", "checked");
5、根據(jù)已知有ID的元素操作沒(méi)有ID的元素:
根據(jù)Parent,FirstChild,Children[1]數(shù)組,多少層級(jí)的元素都能找到。
?
6、獲取Div或其他元素的樣式:
webBrowser1.Document.GetElementById("addDiv").Style;?
7、直接執(zhí)行頁(yè)面中的腳本函數(shù),帶動(dòng)態(tài)參數(shù)或不帶參數(shù)都行:
Object[] objArray = new Object[1]; objArray[0] = (Object)this.labFlightNumber.Text; webBrowser1.Document.InvokeScript("ticketbook", objArray); webBrowser1.Document.InvokeScript("return false");?
8、自動(dòng)點(diǎn)擊、自動(dòng)提交:
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild; btnAdd.InvokeMember("Click");?
9、自動(dòng)賦值,然后點(diǎn)擊提交按鈕的時(shí)候如果出現(xiàn)腳本錯(cuò)誤或一直加載的問(wèn)題,一般都是點(diǎn)擊事件執(zhí)行過(guò)快,這時(shí)需要借助Timer控件延遲執(zhí)行提交按鈕事件:
this.timer1.Enabled = true; this.timer1.Interval = 1000 * 2; private void timer1_Tick(object sender, EventArgs e) {this.timer1.Enabled = false;ClickBtn.InvokeMember("Click");//執(zhí)行按扭操作 }?
10、屏蔽腳本錯(cuò)誤:
?
11、自動(dòng)點(diǎn)擊彈出提示框:
?WebBrowser頁(yè)面加載完畢之后,在頁(yè)面中進(jìn)行一些自動(dòng)化操作的時(shí)候彈出框的自動(dòng)點(diǎn)擊(屏蔽)
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {//自動(dòng)點(diǎn)擊彈出確認(rèn)或彈出提示IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //彈出確認(rèn)vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//彈出提示//下面是你的執(zhí)行操作代碼 }?
12、獲取網(wǎng)頁(yè)中的Iframe,并設(shè)置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、網(wǎng)頁(yè)中存在Iframe的時(shí)候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一樣,前者是主框架的Url,后者是當(dāng)前活動(dòng)框口的Url。
14、讓控件聚焦
?
15、打開(kāi)本地網(wǎng)頁(yè)文件
?
16、獲取元素、表單
//根據(jù)Name獲取元素 public HtmlElement GetElement_Name(WebBrowser wb,string Name) {HtmlElement e = wb.Document.All[Name];return e; }//根據(jù)Id獲取元素 public HtmlElement GetElement_Id(WebBrowser wb, string id) {HtmlElement e = wb.Document.GetElementById(id);return e; }//根據(jù)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; }//設(shè)置元素value屬性的值 public void Write_value(HtmlElement e,string value) {e.SetAttribute("value", value); }//執(zhí)行元素的方法,如:click,submit(需Form表單名)等 public void Btn_click(HtmlElement e,string s) {e.InvokeMember(s); }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lanzhi/p/6468119.html
總結(jié)
以上是生活随笔為你收集整理的C#中的WebBrowser控件的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java正则表达式的菜鸟使用分析
- 下一篇: A English version fo