【问题标题】:Implicit / explicit wait not waiting for specified amount of time隐式/显式等待不等待指定的时间
【发布时间】:2021-03-25 08:50:27
【问题描述】:
我正在尝试对文本字段执行 sendKeys(),这可以通过 Thread.sleep() 来完成(我想避免)。现在我使用了 5 到 10 秒的隐式等待,但执行显然没有等待那段时间。在 elementToBeClickable() 的预期条件下添加显式等待会导致类似的间歇性故障。
【问题讨论】:
标签:
java
selenium
selenium-webdriver
webdriverwait
implicitwait
【解决方案1】:
如果您能够在调用 Thread.sleep() 后对文本字段调用 sendKeys(),则本质上意味着真正的问题在于 implicit wait 和/或 WebDriverWait 的实现
深入了解
在与基于JavaScript、ReactJS、jQuery、AJAX、Vue.js、Ember.js、GWT 等的应用程序元素交互时implicit wait isn't that effective。
在这种情况下,您可以选择完全使用remove implicit wait 和WebDriverWait,因为Waits 的文档清楚地提到:
警告:不要混合隐式和显式等待。这样做可能会导致无法预测的等待时间。例如,设置 10 秒的隐式等待和 15 秒的显式等待可能会导致 20 秒后发生超时。
解决方案
首先您需要将隐式等待重新配置为0,如下所示:
-
Python:
driver.implicitly_wait(0)
-
Java:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
-
点网:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
改为将WebDriverWait 诱导为elementToBeClickable(),如下所示:
-
Python:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "elementID"))).send_keys("Debajyoti Sikdar")
-
Java:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("elementID"))).sendKeys("Debajyoti Sikdar");
-
点网:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.entry#ng-touched[id='limit'][name='limit']"))).SendKeys("Debajyoti Sikdar");
参考
您可以在以下位置找到详细讨论:
【解决方案2】:
在定义显式等待时,请确保您添加了正确的预期条件。
您可以查看此“https://itnext.io/how-to-using-implicit-and-explicit-waits-in-selenium-d1ba53de5e15”。