精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
C#中的WebBrowser控件的使用

C#中的WebBrowser控件的使用

關鍵字:C# WebBrowser

作者:txw1958

原文:http://www.cnblogs.com/txw1958/archive/2012/09/24/CSharp-WebBrowser.html

 

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:獲取當前正在瀏覽的網址的UriReadyState:獲取瀏覽的狀態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);}
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
delphi WebBrowser的使用方法詳解(四)
C#模擬網站登陸_C#builder_積木群組
WebBrowser控件的window.close 問題
引用 js在IE和FF的區別 - Neil的日志 - 網易博客
C# WebBrowser強制使新窗口網頁只在WebBrowser打開
VB關于webbrowser相關操作大全
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 盖州市| 鸡泽县| 房山区| 安西县| 庆云县| 屯门区| 潮州市| 华亭县| 宁都县| 镇宁| 阳西县| 岳普湖县| 岳阳市| 咸阳市| 黑山县| 宜春市| 黔西县| 康乐县| 南宫市| 富蕴县| 石楼县| 天台县| 茶陵县| 雷州市| 浦北县| 黔江区| 德江县| 民和| 秦皇岛市| 广丰县| 天门市| 齐河县| 高唐县| 金山区| 太湖县| 平阳县| 上虞市| 海口市| 来安县| 皮山县| 鹤庆县|