2014-04-11 177 views
2

我加了一些断点我的测试工作在调试模式。但是在删除断点之后,我得到了Timeout异常。我用了Thread.Sleep和它的工作是否有任何的替代品,而不是使用了Thread.Sleep硒的webdriver超时异常

 [Test] 
     public void OpenStockControl() 
     { 
      driver.Navigate().GoToUrl("http://testdeneme.com"); 
      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1)); 
      IWebElement userName = driver.FindElement(By.Id("LoginNew1_tU")); 
      userName.SendKeys("TEST3"); 
      IWebElement userPassword = driver.FindElement(By.Id("LoginNew1_tP")); 
      userPassword.SendKeys("A7535"); 
      IWebElement buttonSubmit = driver.FindElement(By.Id("LoginNew1_bGo")); 
      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); 
      buttonSubmit.Click(); 

      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20)); 
      SwitchWindow("ABC MA", driver); 
      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20)); 

      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
      IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("ext-gen54"))); 
      element.Click(); 

      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); 
      IWebElement element2 = wait.Until(ExpectedConditions.ElementExists(By.Id("ext-gen207"))); 
      element2.Click(); 

      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); 
      IWebElement element3 = wait.Until(ExpectedConditions.ElementExists(By.Id("ext-gen260"))); 
      element3.Click(); 
     } 
+0

我们需要看到一个例子。向我们展示一些失败的代码。如果您使用Sleeps并且它们工作正常,则意味着您有时间问题,应该正确等待页面上发生或发生的特定事件。 – Arran

+0

谢谢,我添加了我的示例。这种方式不起作用... – bilgestackoverflow

+1

它特别是一贯地失败了吗?此外,你应该知道,调用driver.Manage()。超时()。ImplicitlyWait()实际上并没有等待像Thread.sleep()方法做,它只是设置的驱动程序最长等待时间为隐等待。只需在代码开始时调用它(传入20秒参数)就足够了。阅读上ImplicitlyWait和WebDriverWait类 –

回答

0

你看着http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp?我发现,我加入自己的方法,如布尔WaitForPage([webdriver的],[页],[时间])及其配套WaitForNotPage(...)解决了许多的超时问题。显式等待和隐式等待可以解决其中的很多问题,但您仍然会发现自己偶尔会使用Thread.Sleep()。

0

你的等待(WebDriverWait)正在等待10秒出现的元件。 ExpectedCondition不符合,因此抛出异常。您可以增加WebDriverWait构造函数的调用时间。