Selenium在找到元素后抛出超时

loading...


0

有没有人在webdriver找到所需元素时遇到问题,将文本键入其中,然后抛出WebDriverTimeout异常,说它需要花费太多时间才能找到刚刚发送文本的完全相同的元素?如果我在try-catch中包装这个代码块来捕获超时异常,那么测试会继续成功,但它似乎不是这样做的 Health 方式 .

我正在使用c#selenium和chromedriver 2.44

Update: 初始等待配置和操作本身:

var driver = new ChromeDriver();
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(20);
driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(3);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);

...
var searchLocator = By.Id("searchByName");
driver.GetElement(searchLocator, 20, element => element.Displayed).SendKeys("test");

GetElement扩展:

public static IWebElement GetElement(this IWebDriver driver, By locator, double timeout, Func<IWebElement, bool> condition)
{
  var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
  wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
  wait.Until(drv =>
    { 
      var element = driver.FindElement(locator);
      return condition(element);
    });

   return driver.FindElement(locator);
}

堆栈跟踪:

Result StackTrace:    
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.SendKeys(String text)
at Tests.Steps.SearchSteps.WhenTheUserFillsTheSearchFieldWith(String searchFieldName, String data) in C:\...Tests\Steps\SearchSteps.cs:line 64
at lambda_method(Closure , IContextManager , String , String )
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
at Tests.Features.SearchFeature.ScenarioCleanup()
at Tests.Features.SearchFeature.CheckSearch() in C:\...Tests\Features\Search.feature:line 167
Result Message:    
OpenQA.Selenium.WebDriverTimeoutException : timeout
(Session info: chrome=70.0.3538.110)
(Driver info: chromedriver=2.44.609538
(b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 6.3.9600 x86_64)

loading...

1回答

  • 0

    您应该能够使用驱动程序上的manage方法设置各种超时 Span . 其中一个应该做的伎俩:

    driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
    

    您还可以在ctor中指定超时,例如:

    var driver = new ChromeDriver (pathToDriver, TimeSpan.FromSeconds (30))
    

    在构造函数的情况下,超时是命令超时 . 不确定哪一个适合你 . 可能你需要的是隐含的东西 .

评论

暂时没有评论!