Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium C# Timeout Exception

i'm starting to write autotests and have a trouble with Selenium timeout error, when Selenium (Xpath) can't proceed element

private void CheckLogin(IWebDriver driver)
    {   
        var driver = new ChromeDriver();
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000));
        driver.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']")).Click();
        var element = driver.FindElement(By.XPath(".//*/span[contains(text(),'code')]"));
        if (element != null && dealer.Displayed)
        {
            System.Diagnostics.Debug.WriteLine("Element is shown");
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("Element is not shown");
            driver.FindElement(By.XPath(".//*[@id='s2id_autogen5']/a")).Click();
            driver.FindElement(By.XPath(".//*[@id='body']/a")).Click();
            driver.FindElement(By.XPath(".//*[@id='s2id_autogen6_search']")).SendKeys(ENTER);
        }

    }

So, when element in block if{} has been found (text contains value "code"), it works ok, but when it hasn't been found (text doesn't contain value "code"), system sends Timeout error, i tried to use try/catch structure but it didn't help. The same issue for ChromeDriver() and for FirefoxDriver().

Exception:

OpenQA.Selenium.WebDriverException occurred
  HResult=0x80131500
  Message=The HTTP request to the remote WebDriver server for URL http://localhost:56939/session/ timed out after 60 seconds.
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
   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.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
   at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
like image 777
Sergey Gusak Avatar asked Aug 10 '26 17:08

Sergey Gusak


1 Answers

You're getting the Timeout error because you have added the implicit wait. Let's take it on your example:

var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000));
driver.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']")).Click();

In this case, you're 'telling' Selenium to wait the specified timeout before it will search the element you want to be found. If the element was not found, you will get the Timeout error.

var driver = new ChromeDriver();
driver.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']")).Click();

In this case you're telling Selenium to get the element automatically, so in case it will not be found, you will get a NoSuchElementException, with no timeouts because there's no time to wait before the element to be present.

And if we're expanding it to the Explicit wait:

var driver = new ChromeDriver();
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 1000));
wait.Until(d => d.FindElement(By.XPath(".//div[@class='modal fade in']//button[text()='Close']"))).Click();

In this case you're telling Selenium to keep searching for the element until it is found but no longer than the maximum timeout specified as a param to WebDriverWait.

like image 166
Cosmin Avatar answered Aug 12 '26 07:08

Cosmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!