WatiN.Core.IE组件在打开URL时出现Internet Explorer繁忙时出现超时

Ank*_*Roy 15 c# watin

我正在使用WATin IE组件浏览特定网站On StartBrowsing按钮点击事件我正在初始化WatiN.Core.IE的对象并传递网站URL以打开网站,如下面的代码片段所示: -

WatiN.Core.IE ie;  

private void btnStartBrowsing_Click(object sender, EventArgs e)
{          
  ie = new IE(URLs.mainURL);
}
Run Code Online (Sandbox Code Playgroud)

有时或者说我应该说5/10次我收到此错误: - "Internet Explorer忙时超时"

如何解决这个问题?

alo*_*onp 13

在初始化ie对象之前尝试增加watin超时可能是竞争条件.

像这样:

   Settings.AttachToBrowserTimeOut = 240;
   Settings.WaitUntilExistsTimeOut = 240;
   Settings.WaitForCompleteTimeOut = 240;             
Run Code Online (Sandbox Code Playgroud)


Nei*_* T. 9

IE实例可能未正确处理.您需要检查任务管理器,看看您运行了多少个打开的IEXPLORE.EXE进程.如果你有两个以上,我会建议实现一个using块,然后再次检查任务管理器:

using (IE ie = new IE(URLs.mainURL)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

一旦您初始化了"主"浏览器实例变量,更好的解决方案是使用AttachTo方法:

private IE ie = new IE();

public void btnStartBrowsing_Click(object sender, EventArgs e)
{
    using ie2 = ie.AttachTo<IE>(Find.ByUrl(URLs.mainURL))
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

更新: 由于您需要在会话期间访问浏览器,我强烈建议使用单例对象来容纳浏览器对象:

public sealed class BrowserIE
{
    static readonly IE _Instance = new IE();

    static BrowserIE()
    {
    }

    BrowserIE()
    {
    }

    public static IE Instance
    {
        get { return _Instance; }
    }
}
Run Code Online (Sandbox Code Playgroud)

浏览器只打开一次,您可以随时访问它,因为您不必手动实例化它.要使用它,只需在每次需要访问浏览器时调用Instance属性:

BrowserIE.Instance.GoTo(URLs.mainURL);
Divs headerDivs = BrowserIE.Instance.Divs.Filter(Find.ByClass("header"));
Run Code Online (Sandbox Code Playgroud)