C# 抛出System.Runtime.InteropService.COMException并关闭Internet Explorer
我正在使用C# 抛出System.Runtime.InteropService.COMException并关闭Internet Explorer,c#,shdocvw.internetexplorer,C#,Shdocvw.internetexplorer,我正在使用SHDocVw.InternetExplorer打开并控制一个浏览器窗口,以便在内部网站上实现自动化。软件通常按需要运行,但浏览器偶尔会关闭。我已经找到了导致这种情况发生的代码行,但是我想不出解决问题的方法 我的原始代码如下所示: private InternetExplorer window; public HTMLDocument Document { get { try { return (HTMLD
SHDocVw.InternetExplorer
打开并控制一个浏览器窗口,以便在内部网站上实现自动化。软件通常按需要运行,但浏览器偶尔会关闭。我已经找到了导致这种情况发生的代码行,但是我想不出解决问题的方法
我的原始代码如下所示:
private InternetExplorer window;
public HTMLDocument Document
{
get
{
try
{
return (HTMLDocument)window.Document;
}
catch (System.Runtime.InteropServices.COMException)
{
return null;
}
}
}
但是,每当调用window.Document
导致COMException
时,浏览器就会关闭。我尝试创建一个isReady布尔值,当我尝试导航离开当前页面时设置为false,当触发NavigateComplete2
事件时设置为true,但如果浏览器询问“您确定要离开此页面吗?”这意味着我无法与这些弹出窗口交互,则不会触发
因此,我尝试将代码更改为:
private InternetExplorer window;
public HTMLDocument Document
{
get
{
try
{
if (window.Busy) return null;
return (HTMLDocument)window.Document;
}
catch (System.Runtime.InteropServices.COMException)
{
return null;
}
}
}
虽然我经历了较少的崩溃,但当窗口中出现COMException
异常时,它仍会偶尔关闭浏览器。Busy
呼叫
由于NavigateComplete2
在浏览器和InternetExplorer中断导航时不会触发。繁忙的也会导致浏览器关闭,因此我不知道如何解决
编辑:
我现在也尝试过:window.ReadyState==tagREADYSTATE.ReadyState\u COMPLETE
但它在window.ReadyState
上抛出相同的COMException
,并使窗口崩溃