WatiN.Core.IE component giving Timeout while Internet Explorer busy error while opening a URL
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 开发者_StackOverflowExplorer busy"
How to get around this problem??
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;
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"));
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;
}
}
}
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.
精彩评论