【问题标题】:Selenium throws timeout after locating the elementSelenium 定位元素后抛出超时
【发布时间】:2018-12-11 05:51:04
【问题描述】:

当 webdriver 找到需要的元素,在其中键入文本,然后抛出 WebDriverTimeout 异常,说需要太多时间才能找到它刚刚发送文本到的完全相同的元素时,有没有人遇到过这个问题? 如果我将此代码块包装在捕获超时异常的 try-catch 中,则测试会成功进行,但这似乎不是一种健康的方法。

我正在使用 c# selenium 和 chromedriver 2.44

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

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)

【问题讨论】:

  • 发布您的代码和完整的堆栈跟踪。
  • 用相关的HTML代码试验错误堆栈跟踪更新问题
  • 发生错误的原因有很多。最好使用像wireshark或fiddler这样的嗅探器来捕获数据。从错误中您无法判断是否没有返回任何内容。同样使用 http,您需要检查状态并查看是否完成了 200。 http 有 1.0(流模式)和 1.1(块模式)。块模式要求您发送下一个块消息,如果您不发送下一个块,则会超时。
  • 错误在哪一行抛出? Stacktrace 没有帮助,因为 GetElement 没有出现在其中。
  • @Meta-Knight 它似乎在步骤的 SendKeys() 部分中断

标签: c# selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

您应该能够使用驱动程序上的管理方法设置各种超时跨度。其中之一应该可以解决问题:

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

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

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

在构造函数的情况下,超时是命令超时。不确定哪一个适合你。可能你需要的是隐式等待。

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2016-09-16
    • 2014-11-04
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多