Selenium C# ITakesScreenshot 尝试在 catch 块中截取屏幕截图时超时
Selenium C# ITakesScreenshot timed out while trying to take a screenshot in catch block
我在尝试在测试失败时截取屏幕截图时遇到问题。尝试在失败情况下截取屏幕截图时出现超时错误。它在 try 块中工作正常,但在 catch 块中超时。任何帮助将不胜感激。
下面是截图方法:
public class Logging
{
public static void ErrorScreenshot()
{
//Take the screenshot
Screenshot ssh = ((ITakesScreenshot)Driver.BrowserInstance).GetScreenshot();
//Save the screenshot
ssh.SaveAsFile("C:/Users/", ScreenshotImageFormat.Png);
}
}
这是我的测试方法
public static bool FindElement
{
get
{
try
{
var element = Driver.BrowserInstance.FindElement(By.XPath(" "));
if (element != null)
{
return true;
}
}
catch (Exception ex)
{
Logging.ErrorScreenshot();
Logging.Error("Not able to find element" + ex.ToString());
}
return false;
}
}
当它找不到元素时,它会进入 catch 块,Logging.ErrorScreenshot 方法会抛出超时异常。
Error details:
OpenQA.Selenium.WebDriverException
HResult=0x80131500
Message=The HTTP request to the remote WebDriver server for URL http://localhost:55418/session/f3dbde1645dd91e453c5823d72199ea9/screenshot timed out after 60 seconds.
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
at AvbLinqAutoFramework.Logging.ErrorScreenshot() in C:\Users\Logging.cs:line 66
at DashboardPage.get_Verifylogin() in C:\Users\DasboardPage.cs:line 65
at Tests() in C:\Users\SmokeTests\Tests.cs:line 33
Inner Exception 1:
WebException: The operation has timed out
通过使用不同的选择器 (CssSelector) 和 WebDrverWait 解决了这个问题。最初的异常是超时,同时无法使用 XPath 找到元素。
public static bool FindElement
{
get
{
try
{
WebDriverWait wait = new WebDriverWait(Driver.BrowserInstance, TimeSpan.FromSeconds(10));
var inputspcmodel = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector($"div[id ='modelSearch-container'] div[class='k-widget k-multiselect k-multiselect-clearable'] div input")));
inputspcmodel.Click();
return true;
}
catch (Exception ex)
{
Logging.Error("Not able to find element " + ex.ToString());
Logging.ErrorScreenshot();
}
return false;
}
}
我在尝试在测试失败时截取屏幕截图时遇到问题。尝试在失败情况下截取屏幕截图时出现超时错误。它在 try 块中工作正常,但在 catch 块中超时。任何帮助将不胜感激。
下面是截图方法:
public class Logging
{
public static void ErrorScreenshot()
{
//Take the screenshot
Screenshot ssh = ((ITakesScreenshot)Driver.BrowserInstance).GetScreenshot();
//Save the screenshot
ssh.SaveAsFile("C:/Users/", ScreenshotImageFormat.Png);
}
}
这是我的测试方法
public static bool FindElement
{
get
{
try
{
var element = Driver.BrowserInstance.FindElement(By.XPath(" "));
if (element != null)
{
return true;
}
}
catch (Exception ex)
{
Logging.ErrorScreenshot();
Logging.Error("Not able to find element" + ex.ToString());
}
return false;
}
}
当它找不到元素时,它会进入 catch 块,Logging.ErrorScreenshot 方法会抛出超时异常。
Error details:
OpenQA.Selenium.WebDriverException
HResult=0x80131500
Message=The HTTP request to the remote WebDriver server for URL http://localhost:55418/session/f3dbde1645dd91e453c5823d72199ea9/screenshot timed out after 60 seconds.
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
at AvbLinqAutoFramework.Logging.ErrorScreenshot() in C:\Users\Logging.cs:line 66
at DashboardPage.get_Verifylogin() in C:\Users\DasboardPage.cs:line 65
at Tests() in C:\Users\SmokeTests\Tests.cs:line 33
Inner Exception 1:
WebException: The operation has timed out
通过使用不同的选择器 (CssSelector) 和 WebDrverWait 解决了这个问题。最初的异常是超时,同时无法使用 XPath 找到元素。
public static bool FindElement
{
get
{
try
{
WebDriverWait wait = new WebDriverWait(Driver.BrowserInstance, TimeSpan.FromSeconds(10));
var inputspcmodel = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector($"div[id ='modelSearch-container'] div[class='k-widget k-multiselect k-multiselect-clearable'] div input")));
inputspcmodel.Click();
return true;
}
catch (Exception ex)
{
Logging.Error("Not able to find element " + ex.ToString());
Logging.ErrorScreenshot();
}
return false;
}
}