如何检查是否在Selenium C#中打开了Firefox驱动程序?

时间:2014-12-03 23:41:03

标签: c# firefox selenium-webdriver

我正在使用Selenium Firefox WebDrivers在WinForms C#中工作,我正在使用这样的代码来关闭驱动程序:

driver.Close();

但是我经常遇到驱动程序已经关闭的错误。如何检查驱动程序是否已打开(或已关闭)?

此外,如果有人知道我怎么能在c#中隐藏它?喜欢隐藏到FireFox窗口。

3 个答案:

答案 0 :(得分:0)

您可以在webDriver中查询当前窗口句柄。如果存在,则调用quit。

if(!String.IsNullOrEmpty(driver.CurrentWindowHandle))
{
     driver.Quit();
}

答案 1 :(得分:0)

使用try catch块。

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.
}

答案 2 :(得分:0)

如果在驱动程序关闭后尝试调用driver.CurrentWindowHandle,则会引发

WebDriverException。最好使异常尽可能具体,所以赶上WebDriverException。

try
{
    driver.Quit();
}
catch (WebDriverException e)
{
    // Happens when call Quit but driver already closed.  Can be ignored.
}