首页 文章

如果找不到元素,为什么Selenium会给出超时异常?

提问于
浏览
2

我在Selenium C#WebDriver中尝试了以下代码片段 . (版本2.50) . 我用多个函数测试它(By.Xpath,By.ClassName,By.CssSelector等)

var webDriver = new FirefoxDriver();
webDriver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 0, 60));
webDriver.Navigate().GoToUrl("http://google.com");
var resultElement = webDriver.FindElement(By.ClassName("NonExistingClass"));

为什么这不会只返回一个空集合或null或甚至NoSuchElementException?我认为它不应该返回超时异常 .

例外:

“WebDriver.dll中出现'OpenQA.Selenium.WebDriverException'类型的例外但未在用户代码中处理附加信息:对URL http:// localhost:7055 / hub / session /的远程WebDriver服务器的HTTP请求19e937df-9d51-4624-a700-33f0ec6be98c / element在60秒后超时 . “

2 回答

  • 0

    您提供了一段时间等待 . 因此驱动程序将等待该时间,然后如果驱动程序无法在页面上找到该元素,则会抛出超时异常 .

    Just remove specified wait timespan 来解决这个问题 .

    我发布这个作为这个问题的答案,即使他的评论解决了这个问题 .

  • 4
    IWebDriver driver = new FirefoxDriver();
    driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 0, 60));
    driver.Navigate().GoToUrl("http://google.com");
    var resultElement = driver.FindElement(By.ClassName("NonExistingClass"));
    

相关问题