【问题标题】:Selenium C# ITakesScreenshot timed out while trying to take a screenshot in catch blockSelenium C# ITakesScreenshot 在尝试在 catch 块中截屏时超时
【发布时间】:2020-02-12 16:49:02
【问题描述】:

我在尝试在测试失败时截屏时遇到问题。尝试在失败情况下截屏时出现超时错误。它在 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

【问题讨论】:

  • 原始异常是什么?您能否将记录异常的行移到屏幕截图行上方并发布原始异常是什么?
  • @GregBurghardt 感谢您为我指明正确的方向。这就是我最初在屏幕截图上方记录异常但不认为问题可能是原始异常的方式。由于我使用的是 XPath contains,它实际上是超时的。我使用了另一个选择器并且工作正常。
  • 好的。嗯。我只是希望最初的异常可以指出真正的问题。虽然如果最初的异常是超时,那么这意味着 Selenium 失去了与浏览器的连接。如果没有连接到浏览器,您将无法截取屏幕截图。您可能正在尝试解决错误的问题。

标签: c# selenium webdriver screenshot


【解决方案1】:

通过使用不同的选择器 (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;
            }

        }

【讨论】:

  • 您的 CSS 选择器看起来不像 CSS 选择器...它看起来更像 XPath 语法。我会将其更改为#modelSearch-container div.k-widget.k-multiselect.k-multiselect-clearable div input。它使阅读更容易,但也删除了类的确切字符串比较。 # 表示 ID,. 表示类名。
  • 另外,您的 FindElement 方法正在单击,考虑到名称,这似乎很奇怪。您可能希望对其进行更改以更清楚地了解该方法实际在做什么。如果你使用的是 NUnit 之类的库,你可以等到测试结束,检查测试是否失败,如果是,则截屏。这将确保您获得每个失败的屏幕截图,而无需将代码放在除FindElement之外的所有可能位置
  • @JeffC 感谢您的反馈。我将清理 CSS 选择器。当我开始这个测试时,FindElement 方法名称只是一个“测试方法名称”。我已将其更改为实际有意义的名称。
猜你喜欢
  • 1970-01-01
  • 2010-11-23
  • 2021-11-26
  • 2010-12-25
  • 1970-01-01
  • 2023-03-18
  • 2020-05-12
  • 1970-01-01
  • 2013-09-23
相关资源
最近更新 更多