I'm working in WinForms C# with Selenium Firefox WebDrivers and I'm using a code like this to close the driver:
driver.Close();
But I'm often getting errors that the driver is already closed. How can I check if the driver is opened (or already closed) ?
Also if anyone here knows how could I hide it in c#? Like hide to FireFox Window.
You can query webDriver for a current window handle. If it exists, then call quit.
if(!String.IsNullOrEmpty(driver.CurrentWindowHandle))
{
driver.Quit();
}
use a try catch block.
try
{
driver.Close();
//driver will be closed if it is opened
}
catch(Exception ex)
{
//if driver is already closed, your code will not crash due to catch block.
}
WebDriverException is thrown if try to call driver.CurrentWindowHandle after driver closed. Good idea to make exception as specific as possible, so catch WebDriverException.
try
{
driver.Quit();
}
catch (WebDriverException e)
{
// Happens when call Quit but driver already closed. Can be ignored.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.