【发布时间】: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