Search code examples
c#seleniumwebdriverscreenshot

Selenium C# ITakesScreenshot timed out while trying to take a screenshot in catch block


I am having an issue while trying to take screenshot upon test failure. I get timed out error when try to take the screenshot on failure condition. It works fine in try block but timed out in catch block. Any help would be appreciate.

Below is the method to take screenshot:

 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);
             } 
          }

This is my test method

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

  }

when it is not able to find the element it goes to catch block and there Logging.ErrorScreenshot method throws a timed out exception.

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

Solution

  • Fixed this issue by using different selector (CssSelector) and WebDrverWait. The original exception was timing out while not able to find the element using 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;
                }
    
            }