简体   繁体   English

Selenium C#Timeout Exception

[英]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 当Selenium(Xpath)无法继续时,我开始编写自动测试并遇到Selenium超时错误的问题

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. 因此,当块中的元素if {}已找到(文本包含值“code”)时,它可以正常工作,但是当找不到它时(文本不包含值“code”),系统发送Timeout错误,我试图使用try / catch结构,但它没有帮助。 The same issue for ChromeDriver() and for FirefoxDriver(). ChromeDriver()和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)

You're getting the Timeout error because you have added the implicit wait. 您收到了Timeout错误,因为您已添加隐式等待。 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. 在这种情况下,你'告诉'Selenium在搜索你想要找到的元素之前等待指定的超时。 If the element was not found, you will get the Timeout error. 如果找不到该元素,您将收到Timeout错误。

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. 在这种情况下,你告诉Selenium自动获取元素,所以如果找不到它,你将得到NoSuchElementException ,没有超时,因为在元素出现之前没有时间等待。

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 . 在这种情况下,您告诉Selenium继续搜索元素,直到找到它,但不超过指定为WebDriverWait的参数的最大超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM