1:Winform应用通过mshtml操作IE浏览器DOM时,第一次运行正常,点击第二次时错误信息如下

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in openie01.exe

Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL。

If there is a handler for this exception, the program may be safely continued.

Additional information: 对 COM 组件的调用返回了错误 HRESULT E_FAIL

2:出现异常的代码 

为:mshtml.HTMLDocument doc = ie.Document;
SHDocVw.InternetExplorer ie = getInternetExploer(url);
            if (null == ie) 
            {
                //如果没有打开,则进行打开操作,并获取ie对象
                ie = new SHDocVw.InternetExplorer();
                ie.Navigate(url);
                ie.Visible = true;

                ie.DocumentComplete += ie_DocumentComplete;

                compWait();
            }

            //操作DOM进行模拟登陆
            mshtml.HTMLDocument doc = ie.Document;

 

第一次运行正常,是因为进行了加载完成的判断

private void ie_DocumentComplete(object pDisp, ref object URL)
        {
            ie_Read = true;
        }

        private void compWait()
        {
            while (ie_Read != true)
            {
                Application.DoEvents();
            }
        }

第二次运行异常,是因为ie_Read这个变量没有置位导致的。

3:解决方法

在程序运行完之后将ie_Read置位

 //将标识复位
            ie_Read = false;

 

相关文章:

  • 2022-12-23
  • 2021-07-25
  • 2021-07-12
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2021-10-17
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案