I'm using C# / Selenium 3 and the Microsoft Chromium Edge Webdriver to scrape a web page and then deliver data to another application. I need to check if the user has closed the web browser. Is there a quick way of doing this? I came up with the code below but the problem is that if the web browser is closed then _webDriver.CurrentWindowHandle takes 4 seconds or more before it throws an exception.
public bool IsOpen
{
get
{
if (!this._isDisposed)
{
try
{
_ = this._webDriver.CurrentWindowHandle;
return true;
}
catch
{
// ignore.
}
}
return false;
}
}
In the end I came up with the following solution: I used the extension method (shown below ) to get a .Net Process object for the Web Browser. To check if the browser is still open I just check the property process.HasExited. If this is true then the user has closed the browser. This method does not call Selenium so even if the browser is closed the result is near instant.