• C#的WebBrowser的操作与注意事项,不断更新


    1.在Winform里使用WebBrowser,要对Form1.cs添加一些东西:

        1.1 在“public partial class Form1 : Form”上方,添加:

    1 [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    2 [System.Runtime.InteropServices.ComVisibleAttribute(true)]

        1.2 在Form1的Shown事件中,添加:

    1 this.UI_webBrowser.ObjectForScripting = this;

      

    2.由于WebBrowser是放在Winform界面中,由界面线程(主线程)管理,执行渲染也是主线程,因此,不能把业务逻辑放在主线程中,应该另开一个线程,执行业务逻辑。并通过Invoke来与WebBrowser交互。

      例子:

     1 private void Form1_Shown(object sender, EventArgs e)
     2 {
     3     this._thread_mainLogic = new Thread(this.ThreadFunction_MainLogic);
     4     this._thread_mainLogic.Start();
     5 }
     6 
     7 private void ThreadFunction_MainLogic()
     8 {
     9     Debugger.Log(0, "", "\r\n开始执行业务逻辑\r\n");
    10     this.Invoke( new Action( () => { this.webBrowser.Navigate("http://www.baidu.com");} ) );//通过Invoke来与webBrowser交互
    11     .....
    12 }

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

     1 //以下方法不是线程安全方法
     2 private AutoResetEvent _threadControlEvent_Tool_webBrowser_Navigate = null;
     3 
     4 private void Tool_webBrowser_Navigate(string arg_URL)
     5 {
     6     this._threadControlEvent_Tool_webBrowser_Navigate = new AutoResetEvent(false);
     7     this.Invoke(new Action(() =>
     8     {
     9         this.webBrowser.DocumentCompleted += webBrowser_DocumentCompleted_Tool_webBrowser_Navigate;
    10         this.webBrowser.Navigate(arg_URL);
    11     }));
    12     this._threadControlEvent_Tool_webBrowser_Navigate.WaitOne();
    13     this._threadControlEvent_Tool_webBrowser_Navigate.Close();
    14     this._threadControlEvent_Tool_webBrowser_Navigate.Dispose();
    15 }
    16 
    17 void webBrowser_DocumentCompleted_Tool_webBrowser_Navigate(object sender, WebBrowserDocumentCompletedEventArgs e)
    18 {

            if (this.UI_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                return;
            }

    19     this.webBrowser.DocumentCompleted -= webBrowser_DocumentCompleted_Tool_webBrowser_Navigate;
    20     this._threadControlEvent_Tool_webBrowser_Navigate.Set();
    21 }

    4.根据ID获取按钮,并点击它:(也可作用于网页中的URL链接)

    1 //假设网页里的按钮,ID为"btn"
    2 HtmlElement element_btn = null;
    3 this.Invoke(new Action(() => { element_btn = this.UI_webBrowser.Document.All["btn"]; }));//获取
    4 element_btn.InvokeMember("Click");//点击,此方法为同步方法,可安全使用

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

    1 //假设网页里的输入框,ID为"input"
    2 HtmlElement input = null;
    3 this.Invoke( new Action( () => { input = this.UI_webBrowser.Document.All["input"]; } ) );//获取
    4 input.InnerText = "123";//输入"123"。此方法不为同步方法,需要使用下文的Wait_SafeMode方法。
    5 Tool_Wait_SafeMode();//实现在下文


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

    1 //假设网页里的form,ID为"form2"
    2 HtmlElement form2 = null;
    3 this.Invoke( new Action( () => { form2 = this.UI_webBrowser.Document.Forms["form2"]; } ) );//获取
    4 form_submit.InvokeMember("submit");//提交form2里的内容。此方法为同步方法,可安全使用。

    7.根据ID获取CheckBox,并设置为已选中(Checked)

    1 //假设网页里的CheckBox,ID为"checkbox5"
    2 HtmlElement checkBox5 = null;
    3 this.Invoke( new Action( () => { checkBox5 = this.UI_webBrowser.Document.All["checkbox5"]; } ) );//获取
    4 checkBox5.SetAttribute("Checked", "true");//设置为已选中。此方法为同步方法,可安全使用。


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

     1 //假设网页里,有且仅有这样的一个元素:它有一个名为"value"的属性,属性值为"12345"
     2 bool isFind = false;
     3 HtmlElementCollection htmlElementCollection = null;
     4 this.Invoke( new Action( () => { htmlElementCollection = this.webBrowser.Document.All; } ) );//获取集合
     5 HtmlElement resultElement = null;
     6 
     7 foreach (HtmlElement currentElement in htmlElementCollection)//在集合中遍历所有元素来寻找
     8 {
     9     if (currentElement.GetAttribute("value") == "12345")
    10     {
    11         isFind = true;
    12         resultElement = currentElement;
    13         break;
    14     }
    15 }
    16 
    17 if( ! isFind )
    18 {
    19     对没有找到的情况进行处理;
    20 }

    8+.根据OutHtml来查找元素:

     1 public HtmlElement GetElementByOutHtml(string arg_outHtml, HtmlElementCollection arg_htmlElementCollection = null)
     2         {
     3             if (arg_htmlElementCollection == null)
     4             {
     5                 arg_htmlElementCollection = this.UI_WebBrowser.Document.All;
     6             }
     7             foreach (HtmlElement he in arg_htmlElementCollection)
     8             {
     9                 if (he.OuterHtml == arg_outHtml)
    10                 {
    11                     return he;
    12                 }
    13             }
    14             throw new Exception("找不到outHtml为【" + arg_outHtml + "】的元素。");
    15         }


    9.对网页中的ComboBox进行设置。不推荐这样做,因为SetAttribute是一个没有回应的API,因此建议使用js来进行设置。

    1 //假设网页中存在一个ComboBox,ID为"comboBox123",下拉菜单有两项:
    2 //第一项的ID为1,value为"苹果"
    3 //第二项的ID为2,value为"西瓜"
    4 HtmlElement element_comboBox = null;
    5 this.Invoke( new Action( () => { element_comboBox = this.webBrowser.Document.All["comboBox123"]; } ) );//获取
    6 Tool_Wait_SafeMode();
    7 this.Invoke( new Action( () => { element_comboBox.SetAttribute("value", "2"); } ) );//设置为"西瓜",即value = 2
    8 Tool_Wait_SafeMode();

    10.Tool_Wait_SafeMode

     1 private void Tool_Wait_SafeMode()
     2 {
     3     bool isError = false;
     4     bool isBusy = false;
     5     do
     6     {
     7         this.Invoke(new Action(() => 
     8         {
     9             try
    10             {
    11                 isBusy = this.webBrowser.IsBusy;
    12             }
    13             catch (System.Exception ex)
    14             {
    15                 isError = true;
    16             }
    17         }));
    18         if (isError)
    19         {
    20             Thread.Sleep(errorWaitTime);//建议为2秒以上。这个时间要根据机器性能来设置,必须设置长一些。
    21         }
    22         else
    23         {
    24             if (isBusy)
    25             {
    26                 Thread.Sleep(arg_waitTime);//建议为0.1秒以上。这个时间要根据机器性能来设置,可以设置短一些。
    27             }
    28         }
    29     }
    30     while (isError | isBusy);
    31 }


    11.在网页中执行js代码

        下面的方法是靠谱方法:

        (1).引用Microsoft.mshtml

        (2). using mshtml;

        (3).给WebBrowser使用一下代码:其中s为js代码。这段代码的意思是,屏蔽alert,需要做到navigated事件中。

    void UI_WebBrowser_Navigated_noAlert(object sender, WebBrowserNavigatedEventArgs e)
    {
    IHTMLWindow2 win = (IHTMLWindow2)this.UI_WebBrowser.Document.Window.DomWindow;
    string s = @"function confirm() {";
    s += @"return true;";
    s += @"}";
    s += @"function alert() {}";
    win.execScript(s);
    }

        下面的方法是不靠谱方法

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

     1         #region private void Tool_webBrowser_ExecUserJSScript(string arg_jsCodes)
     2         private AutoResetEvent _threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init = null;
     3         private AutoResetEvent _threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec = null;
     4         private object _returnObj_Tool_webBrowser_ExecUserJSScript = null;
     5 
     6         /// <summary>
     7         /// 用WebBrowser执行JS自定义语句。
     8         /// 1:定义一个js方法,方法名尽量特殊些,以免与html里已存在的js方法重名。这个方法的结尾,一定要使用window.external.NotifyCSharpComplete( msg );才能实现js执行结束后,通知CSharp。把这个方法传递给参数arg_jsFunctionDefineCodes。
     9         /// 2:把这个方法的方法名,传递给参数arg_jsFunctionName。
    10         /// 3: 把这个方法,需要传递的参数,传递给arg_functionArgs。如果不需要传入参数,该字段可以不需要赋值,或赋值为null,或赋值为new object[]{}。
    11         /// 4: 如果js在回调C#时,不需要返回参数,请在js方法里使用window.external.NotifyCSharpComplete( null );如果有返回参数,则可以修改为window.external.NotifyCSharpComplete( 参数变量 );
    12         /// 例子:js方法:function jsFunctionTest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.NotifyCSharpComplete( "运算结果:" + arg3 ); }
    13         /// 则 arg_jsFunctionDefineCodes = "function jsFunctionTest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.NotifyCSharpComplete( \"运算结果:\" + arg3 ); }";
    14         ///    arg_jsFunctionName = jsFunctionTest
    15         ///    如果需要传递的参数为123、456,则arg_functionArgs = new object[] { 123, 456 }
    16         /// 返回值,通过object进行返回。如果object是一个其他类型,则请自行转换。比如:stirng result = (string)Tool_webBrowser_ExecUserJSScript(...);
    17         /// </summary>
    18         /// <param name="arg_jsFunctionDefineCodes">js方法,注意,总长度不能超过1991(总长不能超过2048,程序中会对字符串添加一些内容。)</param>
    19         /// <param name="arg_jsFunctionName">js方法的方法名</param>
    20         /// <param name="arg_functionArgs">js方法的参数列表。如果不需要传入参数,该字段可以不需要赋值,或赋值为null,或赋值为new object[]{}</param>
    21         /// <returns>返回执行结果。注意,默认为返回参数。如果没有返回,请修改js方法,把NotifyCSharpComplete( msg )改为NotifyCSharpComplete( null )</returns>
    22         private object Tool_webBrowser_ExecUserJSScript(string arg_jsFunctionDefineCodes, string arg_jsFunctionName, object[] arg_functionArgs = null)
    23         {
    24             this._returnObj_Tool_webBrowser_ExecUserJSScript = null;
    25             if (arg_jsFunctionDefineCodes.Length > 1991)
    26             {
    27                 throw new Exception("错误:js方法定义的长度超过了1991。");
    28             }
    29             //1.写入js方法。
    30             arg_jsFunctionDefineCodes = "javascript:" + arg_jsFunctionDefineCodes + ";window.external.NotifyCSharpCompleteInit();";
    31             if (arg_jsFunctionDefineCodes.Length >= 2048)
    32             {
    33                 throw new Exception("错误:js方法定义的总长度超过了2048(原始方法 + 添加的内容)。");
    34             }
    35             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init = new AutoResetEvent(false);
    36             this.Invoke(new Action(() =>
    37             {
    38                 this.webBrowser.Navigate(arg_jsFunctionDefineCodes);
    39             }));
    40             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.WaitOne();
    41             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Close();
    42             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Dispose();
    43             //2.执行js方法
    44             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec = new AutoResetEvent(false);
    Thread.Sleep(一个与机器性能有关的值);//i5:小计算量:500ms/中、大计算量:1000ms~1500ms
    45
    this.Invoke(new Action(() => 46 { 47 this.webBrowser.Document.InvokeScript(arg_jsFunctionName, arg_functionArgs); 48 })); 49 this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.WaitOne(); 50 this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Close(); 51 this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Dispose(); Thread.Sleep(一个与机器性能有关的值);//i5:小计算量:500ms/中、大计算量:1000ms~1500ms
    52 //3.返回参数 53 return this._returnObj_Tool_webBrowser_ExecUserJSScript; 54 } 55 56 public void NotifyCSharpCompleteInit() 57 { 58 this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Set(); 59 } 60 61 public void NotifyCSharpComplete(object arg_obj) 62 { 63 this._returnObj_Tool_webBrowser_ExecUserJSScript = arg_obj; 64 this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Set(); 65 } 66 #endregion

    用法例子1:

    1 string jsCmdTest = "function testFunction( msg ) { setTimeout(\"window.external.NotifyCSharpComplete(\\\"返回内容\\\");\", 5000);};";
    2 object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", new object[] {"传入参数"});
    3 string returnStr = returnObj as string;

    用法例子2:

    1 string jsCmdTest = "function testFunction( ) { var a = 122; var b = 244; var c = a + b; window.external.NotifyCSharpComplete(c);};";
    2 object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", null);
    3 int returnInt = (int)returnObj;

    用法例子3:

    1 string jsCmdTest = "function testFunction( ) { window.external.NotifyCSharpComplete(null);};";
    2 object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", null);
    3 string result = "js执行完毕";

    12.屏蔽alert

    (1).引用Microsoft.mshtml

        (2). using mshtml;

        (3).给WebBrowser使用一下代码:其中s为js代码。这段代码的意思是,屏蔽alert,需要做到navigated事件中。

    void UI_WebBrowser_Navigated_noAlert(object sender, WebBrowserNavigatedEventArgs e)
    {
    IHTMLWindow2 win = (IHTMLWindow2)this.UI_WebBrowser.Document.Window.DomWindow;
    string s = @"function confirm() {";
    s += @"return true;";
    s += @"}";
    s += @"function alert() {}";
    win.execScript(s);
    }

       需要注意的是,如果alert发起的位置,是在主html内,则上述办法有效。但如果是iframe内,则先从iframe来访问:

      IHTMLWindow2 win  = (IHTMLWindow2)this.UI_WebBrowser.Document.Window.Frames["contentFrame"].Document.All["xxx"].Document.Window.DomWindow;

    13.屏蔽脚本错误对话框

        this.UI_WebBrowser.ScriptErrorsSuppressed = true;

        注意,这句代码,需要在浏览之前设置。

    14.拦截或获取新窗口

        如果WebBrowser中的网页需要弹出一个新网页,则可以用这种方法。

        首先要引用 %windir%/system32/SHDocVw.dll

        然后要 private SHDocVw.WebBrowser_V1 _web_popUp = null;

        接着,对webBrowser:

        this._web_popUp = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;
        this._web_popUp.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(Web_PopUp_NewWindow);

        最后:

        private void Web_PopUp_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)

        {

            Processed = true;//表示新窗口的逻辑是由该方法处理,不需要再弹出对话框了。如果为false,则仍然会弹出新对话框

            //如果需要浏览,则使用一个新的WebBrowser,然后 newWebBrowser.Navigate( URL ) 就行了。

        }

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

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

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

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

  • 相关阅读:
    HDU 2192 MagicBuilding
    HDU 2148 Score
    HDU 1847 Good Luck in CET4 Everybody!
    往CImageList中加图标列表
    LoadIcon
    OnInitialUpdate 详解
    设备坐标(DP)、客户坐标(Client)、逻辑坐标(LP)
    Web及网络基础学习(一)
    Qt 下QMessageBox下中文乱码问题
    vs2005菜单:选项项目和解决方案
  • 原文地址:https://www.cnblogs.com/xxxteam/p/2964418.html
Copyright © 2020-2023  润新知