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.
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;