Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatiN.Core.IE component giving Timeout while Internet Explorer busy error while opening a URL

Tags:

c#

watin

I am using WATin IE component for browsing a specific website On StartBrowsing button click event I am initializing the object of WatiN.Core.IE and passing the website URL for opening the website as shown in the code snippet below :-

WatiN.Core.IE ie;  

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

Sometimes or rather i should say 5/10 times I am getting this error :- "Timeout while Internet Explorer busy"

How to get around this problem??

like image 326
Ankush Roy Avatar asked Sep 21 '10 10:09

Ankush Roy


4 Answers

It might be a race condition try to increase the watin timeouts before you initialize the ie object.

Like this:

   Settings.AttachToBrowserTimeOut = 240;
   Settings.WaitUntilExistsTimeOut = 240;
   Settings.WaitForCompleteTimeOut = 240;             
like image 53
alonp Avatar answered Oct 18 '22 13:10

alonp


It is possible that the IE instance isn't being disposed properly. You'll want to check Task Manager and see how many open IEXPLORE.EXE processes you have running. If you have more than two, I would suggest implementing a using block and then checking Task Manager again:

using (IE ie = new IE(URLs.mainURL)
{
    ...
}

An even better solution would be to use the AttachTo method once you have initialized a "master" browser instance variable:

private IE ie = new IE();

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

UPDATE: Since you need access to the browser for the duration of the session, I would strongly suggest using a singleton object to house the browser object:

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

    static BrowserIE()
    {
    }

    BrowserIE()
    {
    }

    public static IE Instance
    {
        get { return _Instance; }
    }
}

The browser is only opened one time, and you have access to it whenever you need it, since you don't have to manually instantiate it. To use it, you simply call the Instance property every time you need access to the browser:

BrowserIE.Instance.GoTo(URLs.mainURL);
Divs headerDivs = BrowserIE.Instance.Divs.Filter(Find.ByClass("header"));
like image 33
Neil T. Avatar answered Oct 18 '22 12:10

Neil T.


I use this class to attach to browser. And it works fine.

public class StaticIE : IDisposable
{
    private IE _ie;
    private int _ieThread;
    private string _ieHwnd;
    private int _processId;

    public StaticIE(IE aBrowser)
    {
        SetBrowser(aBrowser);
    }

    public IE Browser
    {
        get
        {
            var currentThreadId = GetCurrentThreadId();
            _ie = Browser.AttachTo<IE>(Find.By("hwnd", _ieHwnd), 1);
            if (currentThreadId != _ieThread)
            {
                _ieThread = currentThreadId;
            }
            return _ie;
        }
    }

    private void SetBrowser(IE aBrowser)
    {
        _ie = aBrowser;
        _ieHwnd = _ie.hWnd.ToString();
        _ieThread = GetCurrentThreadId();
        _processId = _ie.ProcessID;
    }

    private int GetCurrentThreadId()
    {
        return Thread.CurrentThread.GetHashCode();
    }

    public void Dispose()
    {
        if (_ie != null)
        {
            Process.GetProcessById(_processId).Kill();
            _ie = null;
        }
    }
}
like image 38
Pavlo Neiman Avatar answered Oct 18 '22 13:10

Pavlo Neiman


Process[] processes = Process.GetProcessesByName("iexplore");

        foreach (Process process in processes)
        {
            process.Kill();
        }

That above code solved my problem. Simply kills IE on each run, its due to ie still running.

like image 37
scott Avatar answered Oct 18 '22 13:10

scott