当前位置: 移动技术网 > IT编程>开发语言>c# > C#的WebBrowser的操作与注意事项介绍

C#的WebBrowser的操作与注意事项介绍

2019年07月18日  | 移动技术网IT编程  | 我要评论

1.在winform里使用webbrowser,要对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界面中,由界面线程(主线程)管理,执行渲染也是主线程,因此,不能把业务逻辑放在主线程中,应该另开一个线程,执行业务逻辑。并通过invoke来与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开始执行业务逻辑\r\n");
     this.invoke( new action( () => { this.webbrowser.navigate("http://www.baidu.com");} ) );//通过invoke来与webbrowser交互
     .....
 }

3.浏览指定url。注意,此方法为异步方法,需要手动同步。

复制代码 代码如下:

//以下方法不是线程安全方法
 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.根据id获取按钮,并点击它:(也可作用于网页中的url链接)

复制代码 代码如下:

//假设网页里的按钮,id为"btn"
htmlelement element_btn = null;
this.invoke(new action(() => { element_btn = this.ui_webbrowser.document.all["btn"]; }));//获取
element_btn.invokemember("click");//点击,此方法为同步方法,可安全使用

5.根据id获取输入框,并输入内容

复制代码 代码如下:

//假设网页里的输入框,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();//实现在下文

6.根据id获取form,并提交(submit)

复制代码 代码如下:

//假设网页里的form,id为"form2"
htmlelement form2 = null;
this.invoke( new action( () => { form2 = this.ui_webbrowser.document.forms["form2"]; } ) );//获取
form_submit.invokemember("submit");//提交form2里的内容。此方法为同步方法,可安全使用。

7.根据id获取checkbox,并设置为已选中(checked)

复制代码 代码如下:

//假设网页里的checkbox,id为"checkbox5"
htmlelement checkbox5 = null;
this.invoke( new action( () => { checkbox5 = this.ui_webbrowser.document.all["checkbox5"]; } ) );//获取
checkbox5.setattribute("checked", "true");//设置为已选中。此方法为同步方法,可安全使用。


8.根据元素的已知属性,来查找该元素

复制代码 代码如下:

//假设网页里,有且仅有这样的一个元素:它有一个名为"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)//在集合中遍历所有元素来寻找
 {
     if (currentelement.getattribute("value") == "12345")
     {
         isfind = true;
         resultelement = currentelement;
         break;
     }
 }

 if( ! isfind )
 {
     对没有找到的情况进行处理;
 }


9.对网页中的combobox进行设置。注意,以下代码有问题,请勿使用。由于setattribute是一个没有回应的api,因此建议使用js来进行设置。下文中,让webbrowser执行js代码,可以做到有回调。

复制代码 代码如下:

//假设网页中存在一个combobox,id为"combobox123",下拉菜单有两项:
 //第一项的id为1,value为"苹果"
 //第二项的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"); } ) );//设置为"西瓜",即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秒以上。这个时间要根据机器性能来设置,必须设置长一些。
         }
         else
         {
             if (isbusy)
             {
                 thread.sleep(arg_waittime);//建议为0.1秒以上。这个时间要根据机器性能来设置,可以设置短一些。
             }
         }
     }
     while (iserror | isbusy);
 }

11.在网页中执行js代码

    由于让webbrowser执行js,是一个异步过程,并且还需要回调,因此这个功能有些复杂。对此进行了封装,把它封装为了一个同步过程,来方便使用:

复制代码 代码如下:

#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执行js自定义语句。
         /// 1:定义一个js方法,方法名尽量特殊些,以免与html里已存在的js方法重名。这个方法的结尾,一定要使用window.external.notifycsharpcomplete( msg );才能实现js执行结束后,通知csharp。把这个方法传递给参数arg_jsfunctiondefinecodes。
         /// 2:把这个方法的方法名,传递给参数arg_jsfunctionname。
         /// 3: 把这个方法,需要传递的参数,传递给arg_functionargs。如果不需要传入参数,该字段可以不需要赋值,或赋值为null,或赋值为new object[]{}。
         /// 4: 如果js在回调c#时,不需要返回参数,请在js方法里使用window.external.notifycsharpcomplete( null );如果有返回参数,则可以修改为window.external.notifycsharpcomplete( 参数变量 );
         /// 例子:js方法:function jsfunctiontest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.notifycsharpcomplete( "运算结果:" + arg3 ); }
         /// 则 arg_jsfunctiondefinecodes = "function jsfunctiontest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.notifycsharpcomplete( \"运算结果:\" + arg3 ); }";
         ///    arg_jsfunctionname = jsfunctiontest
         ///    如果需要传递的参数为123、456,则arg_functionargs = new object[] { 123, 456 }
         /// 返回值,通过object进行返回。如果object是一个其他类型,则请自行转换。比如:stirng result = (string)tool_webbrowser_execuserjsscript(...);
         /// </summary>
         /// <param name="arg_jsfunctiondefinecodes">js方法,注意,总长度不能超过1991(总长不能超过2048,程序中会对字符串添加一些内容。)</param>
         /// <param name="arg_jsfunctionname">js方法的方法名</param>
         /// <param name="arg_functionargs">js方法的参数列表。如果不需要传入参数,该字段可以不需要赋值,或赋值为null,或赋值为new object[]{}</param>
         /// <returns>返回执行结果。注意,默认为返回参数。如果没有返回,请修改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("错误:js方法定义的长度超过了1991。");
             }
             //1.写入js方法。
             arg_jsfunctiondefinecodes = "javascript:" + arg_jsfunctiondefinecodes + ";window.external.notifycsharpcompleteinit();";
             if (arg_jsfunctiondefinecodes.length >= 2048)
             {
                 throw new exception("错误:js方法定义的总长度超过了2048(原始方法 + 添加的内容)。");
             }
             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.执行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.返回参数
             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(\\\"返回内容\\\");\", 5000);};";
object returnobj = this.tool_webbrowser_execuserjsscript(jscmdtest, "testfunction", new object[] {"传入参数"});
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执行完毕";

总结:使用webbrowser的两个大问题:

1.webbrowser是调用机器上的ie,因此版本、渲染的程序也就取决与ie的版本与渲染器的程序。

2.webbrowser的执行js等很多操作都是异步且无事件回应的,只能自己去估算一个执行时间,来等待。并且等待时间一定要大于js实际执行时间,否则后续代码会出问题。

3.目前,执行js的方式,只能通过浏览器的地址栏。地址栏是有长度限制的。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网