C#的WebBrowser的操作與注意事項(xiàng)介紹
1.在Winform里使用WebBrowser,要對(duì)Form1.cs添加一些東西:
1.1 在“public partial class Form1 : Form”上方,添加:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
1.2 在Form1的Shown事件中,添加:
this.UI_webBrowser.ObjectForScripting = this;
2.由于WebBrowser是放在Winform界面中,由界面線程(主線程)管理,執(zhí)行渲染也是主線程,因此,不能把業(yè)務(wù)邏輯放在主線程中,應(yīng)該另開(kāi)一個(gè)線程,執(zhí)行業(yè)務(wù)邏輯。并通過(guò)Invoke來(lái)與WebBrowser交互。
例子:
private void Form1_Shown(object sender, EventArgs e)
{
this._thread_mainLogic = new Thread(this.ThreadFunction_MainLogic);
this._thread_mainLogic.Start();
}
private void ThreadFunction_MainLogic()
{
Debugger.Log(0, "", "\r\n開(kāi)始執(zhí)行業(yè)務(wù)邏輯\r\n");
this.Invoke( new Action( () => { this.webBrowser.Navigate("http://www.baidu.com");} ) );//通過(guò)Invoke來(lái)與webBrowser交互
.....
}
3.瀏覽指定URL。注意,此方法為異步方法,需要手動(dòng)同步。
//以下方法不是線程安全方法
private AutoResetEvent _threadControlEvent_Tool_webBrowser_Navigate = null;
private void Tool_webBrowser_Navigate(string arg_URL)
{
this._threadControlEvent_Tool_webBrowser_Navigate = new AutoResetEvent(false);
this.Invoke(new Action(() =>
{
this.webBrowser.DocumentCompleted += webBrowser_DocumentCompleted_Tool_webBrowser_Navigate;
this.webBrowser.Navigate(arg_URL);
}));
this._threadControlEvent_Tool_webBrowser_Navigate.WaitOne();
this._threadControlEvent_Tool_webBrowser_Navigate.Close();
this._threadControlEvent_Tool_webBrowser_Navigate.Dispose();
}
void webBrowser_DocumentCompleted_Tool_webBrowser_Navigate(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser.DocumentCompleted -= webBrowser_DocumentCompleted_Tool_webBrowser_Navigate;
this._threadControlEvent_Tool_webBrowser_Navigate.Set();
}
4.根據(jù)ID獲取按鈕,并點(diǎn)擊它:(也可作用于網(wǎng)頁(yè)中的URL鏈接)
//假設(shè)網(wǎng)頁(yè)里的按鈕,ID為"btn"
HtmlElement element_btn = null;
this.Invoke(new Action(() => { element_btn = this.UI_webBrowser.Document.All["btn"]; }));//獲取
element_btn.InvokeMember("Click");//點(diǎn)擊,此方法為同步方法,可安全使用
5.根據(jù)ID獲取輸入框,并輸入內(nèi)容
//假設(shè)網(wǎng)頁(yè)里的輸入框,ID為"input"
HtmlElement input = null;
this.Invoke( new Action( () => { input = this.UI_webBrowser.Document.All["input"]; } ) );//獲取
input.InnerText = "123";//輸入"123"。此方法不為同步方法,需要使用下文的Wait_SafeMode方法。
Tool_Wait_SafeMode();//實(shí)現(xiàn)在下文
6.根據(jù)ID獲取form,并提交(submit)
//假設(shè)網(wǎng)頁(yè)里的form,ID為"form2"
HtmlElement form2 = null;
this.Invoke( new Action( () => { form2 = this.UI_webBrowser.Document.Forms["form2"]; } ) );//獲取
form_submit.InvokeMember("submit");//提交form2里的內(nèi)容。此方法為同步方法,可安全使用。
7.根據(jù)ID獲取CheckBox,并設(shè)置為已選中(Checked)
//假設(shè)網(wǎng)頁(yè)里的CheckBox,ID為"checkbox5"
HtmlElement checkBox5 = null;
this.Invoke( new Action( () => { checkBox5 = this.UI_webBrowser.Document.All["checkbox5"]; } ) );//獲取
checkBox5.SetAttribute("Checked", "true");//設(shè)置為已選中。此方法為同步方法,可安全使用。
8.根據(jù)元素的已知屬性,來(lái)查找該元素
//假設(shè)網(wǎng)頁(yè)里,有且僅有這樣的一個(gè)元素:它有一個(gè)名為"value"的屬性,屬性值為"12345"
bool isFind = false;
HtmlElementCollection htmlElementCollection = null;
this.Invoke( new Action( () => { htmlElementCollection = this.webBrowser.Document.All; } ) );//獲取集合
HtmlElement resultElement = null;
foreach (HtmlElement currentElement in htmlElementCollection)//在集合中遍歷所有元素來(lái)尋找
{
if (currentElement.GetAttribute("value") == "12345")
{
isFind = true;
resultElement = currentElement;
break;
}
}
if( ! isFind )
{
對(duì)沒(méi)有找到的情況進(jìn)行處理;
}
9.對(duì)網(wǎng)頁(yè)中的ComboBox進(jìn)行設(shè)置。注意,以下代碼有問(wèn)題,請(qǐng)勿使用。由于SetAttribute是一個(gè)沒(méi)有回應(yīng)的API,因此建議使用js來(lái)進(jìn)行設(shè)置。下文中,讓W(xué)ebBrowser執(zhí)行js代碼,可以做到有回調(diào)。
//假設(shè)網(wǎng)頁(yè)中存在一個(gè)ComboBox,ID為"comboBox123",下拉菜單有兩項(xiàng):
//第一項(xiàng)的ID為1,value為"蘋果"
//第二項(xiàng)的ID為2,value為"西瓜"
HtmlElement element_comboBox = null;
this.Invoke( new Action( () => { element_comboBox = this.webBrowser.Document.All["comboBox123"]; } ) );//獲取
Tool_Wait_SafeMode();
this.Invoke( new Action( () => { element_comboBox.SetAttribute("value", "2"); } ) );//設(shè)置為"西瓜",即value = 2
Tool_Wait_SafeMode();
10.Tool_Wait_SafeMode
private void Tool_Wait_SafeMode()
{
bool isError = false;
bool isBusy = false;
do
{
this.Invoke(new Action(() =>
{
try
{
isBusy = this.webBrowser.IsBusy;
}
catch (System.Exception ex)
{
isError = true;
}
}));
if (isError)
{
Thread.Sleep(errorWaitTime);//建議為2秒以上。這個(gè)時(shí)間要根據(jù)機(jī)器性能來(lái)設(shè)置,必須設(shè)置長(zhǎng)一些。
}
else
{
if (isBusy)
{
Thread.Sleep(arg_waitTime);//建議為0.1秒以上。這個(gè)時(shí)間要根據(jù)機(jī)器性能來(lái)設(shè)置,可以設(shè)置短一些。
}
}
}
while (isError | isBusy);
}
11.在網(wǎng)頁(yè)中執(zhí)行js代碼
由于讓W(xué)ebBrowser執(zhí)行js,是一個(gè)異步過(guò)程,并且還需要回調(diào),因此這個(gè)功能有些復(fù)雜。對(duì)此進(jìn)行了封裝,把它封裝為了一個(gè)同步過(guò)程,來(lái)方便使用:
#region private void Tool_webBrowser_ExecUserJSScript(string arg_jsCodes)
private AutoResetEvent _threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init = null;
private AutoResetEvent _threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec = null;
private object _returnObj_Tool_webBrowser_ExecUserJSScript = null;
/// <summary>
/// 用WebBrowser執(zhí)行JS自定義語(yǔ)句。
/// 1:定義一個(gè)js方法,方法名盡量特殊些,以免與html里已存在的js方法重名。這個(gè)方法的結(jié)尾,一定要使用window.external.NotifyCSharpComplete( msg );才能實(shí)現(xiàn)js執(zhí)行結(jié)束后,通知CSharp。把這個(gè)方法傳遞給參數(shù)arg_jsFunctionDefineCodes。
/// 2:把這個(gè)方法的方法名,傳遞給參數(shù)arg_jsFunctionName。
/// 3: 把這個(gè)方法,需要傳遞的參數(shù),傳遞給arg_functionArgs。如果不需要傳入?yún)?shù),該字段可以不需要賦值,或賦值為null,或賦值為new object[]{}。
/// 4: 如果js在回調(diào)C#時(shí),不需要返回參數(shù),請(qǐng)?jiān)趈s方法里使用window.external.NotifyCSharpComplete( null );如果有返回參數(shù),則可以修改為window.external.NotifyCSharpComplete( 參數(shù)變量 );
/// 例子:js方法:function jsFunctionTest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.NotifyCSharpComplete( "運(yùn)算結(jié)果:" + arg3 ); }
/// 則 arg_jsFunctionDefineCodes = "function jsFunctionTest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.NotifyCSharpComplete( \"運(yùn)算結(jié)果:\" + arg3 ); }";
/// arg_jsFunctionName = jsFunctionTest
/// 如果需要傳遞的參數(shù)為123、456,則arg_functionArgs = new object[] { 123, 456 }
/// 返回值,通過(guò)object進(jìn)行返回。如果object是一個(gè)其他類型,則請(qǐng)自行轉(zhuǎn)換。比如:stirng result = (string)Tool_webBrowser_ExecUserJSScript(...);
/// </summary>
/// <param name="arg_jsFunctionDefineCodes">js方法,注意,總長(zhǎng)度不能超過(guò)1991(總長(zhǎng)不能超過(guò)2048,程序中會(huì)對(duì)字符串添加一些內(nèi)容。)</param>
/// <param name="arg_jsFunctionName">js方法的方法名</param>
/// <param name="arg_functionArgs">js方法的參數(shù)列表。如果不需要傳入?yún)?shù),該字段可以不需要賦值,或賦值為null,或賦值為new object[]{}</param>
/// <returns>返回執(zhí)行結(jié)果。注意,默認(rèn)為返回參數(shù)。如果沒(méi)有返回,請(qǐng)修改js方法,把NotifyCSharpComplete( msg )改為NotifyCSharpComplete( null )</returns>
private object Tool_webBrowser_ExecUserJSScript(string arg_jsFunctionDefineCodes, string arg_jsFunctionName, object[] arg_functionArgs = null)
{
this._returnObj_Tool_webBrowser_ExecUserJSScript = null;
if (arg_jsFunctionDefineCodes.Length > 1991)
{
throw new Exception("錯(cuò)誤:js方法定義的長(zhǎng)度超過(guò)了1991。");
}
//1.寫入js方法。
arg_jsFunctionDefineCodes = "javascript:" + arg_jsFunctionDefineCodes + ";window.external.NotifyCSharpCompleteInit();";
if (arg_jsFunctionDefineCodes.Length >= 2048)
{
throw new Exception("錯(cuò)誤:js方法定義的總長(zhǎng)度超過(guò)了2048(原始方法 + 添加的內(nèi)容)。");
}
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init = new AutoResetEvent(false);
this.Invoke(new Action(() =>
{
this.webBrowser.Navigate(arg_jsFunctionDefineCodes);
}));
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.WaitOne();
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Close();
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Dispose();
//2.執(zhí)行js方法
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec = new AutoResetEvent(false);
this.Invoke(new Action(() =>
{
this.webBrowser.Document.InvokeScript(arg_jsFunctionName, arg_functionArgs);
}));
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.WaitOne();
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Close();
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Dispose();
//3.返回參數(shù)
return this._returnObj_Tool_webBrowser_ExecUserJSScript;
}
public void NotifyCSharpCompleteInit()
{
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Set();
}
public void NotifyCSharpComplete(object arg_obj)
{
this._returnObj_Tool_webBrowser_ExecUserJSScript = arg_obj;
this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Set();
}
#endregion
用法例子1:
string jsCmdTest = "function testFunction( msg ) { setTimeout(\"window.external.NotifyCSharpComplete(\\\"返回內(nèi)容\\\");\", 5000);};";
object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", new object[] {"傳入?yún)?shù)"});
string returnStr = returnObj as string;
用法例子2:
string jsCmdTest = "function testFunction( ) { var a = 122; var b = 244; var c = a + b; window.external.NotifyCSharpComplete(c);};";
object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", null);
int returnInt = (int)returnObj;
用法例子3:
string jsCmdTest = "function testFunction( ) { window.external.NotifyCSharpComplete(null);};";
object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", null);
string result = "js執(zhí)行完畢";
總結(jié):使用WebBrowser的兩個(gè)大問(wèn)題:
1.WebBrowser是調(diào)用機(jī)器上的IE,因此版本、渲染的程序也就取決與IE的版本與渲染器的程序。
2.WebBrowser的執(zhí)行js等很多操作都是異步且無(wú)事件回應(yīng)的,只能自己去估算一個(gè)執(zhí)行時(shí)間,來(lái)等待。并且等待時(shí)間一定要大于js實(shí)際執(zhí)行時(shí)間,否則后續(xù)代碼會(huì)出問(wèn)題。
3.目前,執(zhí)行js的方式,只能通過(guò)瀏覽器的地址欄。地址欄是有長(zhǎng)度限制的。
- C#之WinForm WebBrowser實(shí)用技巧匯總
- C#的WebBrowser操作frame實(shí)例解析
- 淺析c#中WebBrowser控件的使用方法
- 解決C#中WebBrowser的DocumentCompleted事件不執(zhí)行的實(shí)現(xiàn)方法
- 在C#中 webbrowser的使用心得
- 使用C#處理WebBrowser控件在不同域名中的跨域問(wèn)題
- webBrowser代理設(shè)置c#代碼
- c# 在WebBrowser中用SendMessage模擬鼠標(biāo)點(diǎn)擊
- 使用C# 的webBrowser寫模擬器時(shí)的javascript腳本調(diào)用問(wèn)題
相關(guān)文章
DevExpress之TreeList用法實(shí)例總結(jié)
這篇文章主要介紹了DevExpress之TreeList用法,對(duì)于C#初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08使用C#實(shí)現(xiàn)對(duì)任意區(qū)域任意大小的截圖
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)簡(jiǎn)單的截圖功能,可以對(duì)任意區(qū)域任意大小的截圖,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關(guān)實(shí)現(xiàn)技巧,代碼簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2016-06-06C#的String轉(zhuǎn)換成float防止精度丟失問(wèn)題的解決
這篇文章主要介紹了C#的String轉(zhuǎn)換成float防止精度丟失問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法,涉及C#針對(duì)硬件屬性的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08