Selenium C#ITakesScreenshot尝试在catch块中截取屏幕截图时超时

问题描述 投票:1回答:1

我在尝试通过测试失败时截取屏幕截图时遇到问题。尝试在故障情况下截屏时出现超时错误。它在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;
     }

  }

当无法找到该元素时,它将捕获块,并且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
c# selenium webdriver screenshot
1个回答
0
投票

通过使用其他选择器(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;
            }

        }
© www.soinside.com 2019 - 2024. All rights reserved.