简体   繁体   English

显式等待在 Selenium webdriver 中不起作用

[英]Explicit wait is not working in Selenium webdriver

I am trying to find an element in my page.我试图在我的页面中找到一个元素。 That element will only come if there is any error in the application.只有在应用程序中出现任何错误时,该元素才会出现。 I handled that pretty well, only issue is that it waits for 30 secs (Implicit wait) to move to next step.我处理得很好,唯一的问题是它等待 30 秒(隐式等待)才能进入下一步。 I don't want to wait for 30 secs instead I'll like my script to wait for 3 secs before moving to next step, so I decided to use explicit wait for the same.我不想等待 30 秒,而是希望我的脚本在进入下一步之前等待 3 秒,所以我决定使用显式等待。 Here is my code这是我的代码

WebDriverWait wait = new WebDriverWait(driver, 3);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("errorelement")));

Problem is that the Selenium webdriver is still waiting for that element upto 30 secs before giving an error and moving to next step.问题是 Selenium webdriver 在给出错误并进入下一步之前仍在等待该元素长达 30 秒。 I tried with visibilityOfElementLocated option as well but it's still not working.我也尝试了visibilityOfElementLocated 选项,但它仍然无法正常工作。

Am I missing something here ?我在这里错过了什么吗?

Selenium version:- 2.46.0硒版本:- 2.46.0

Browser :- Mozilla, Chrome浏览器:- Mozilla、Chrome

I'd recommend to set implicit wait to 0, and always use explicit wait:我建议将隐式等待设置为 0,并始终使用显式等待:

driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

Here is a good answer on this topic.这是关于这个主题的一个很好的答案

If you are using implicit waits and explicit waits in the same solution you will have issues from the seleniumhq docs如果您在同一个解决方案中使用隐式等待和显式等待,您将遇到来自 seleniumhq 文档的问题

WARNING: Do not mix implicit and explicit waits.警告:不要混合隐式和显式等待。 Doing so can cause unpredictable wait times.这样做可能会导致不可预测的等待时间。 For example setting an implicit wait of 10s and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.例如,设置 10 秒的隐式等待和 15 秒的显式等待,可能会导致 20 秒后发生超时。


Adding official documentation link and it's screenshot -添加官方文档链接及其截图 -

Please see official Selenium Documentation on mixing Explicit and Implicit waits.请参阅有关混合显式和隐式等待的 Selenium 官方文档。

( https://www.selenium.dev/documentation/en/webdriver/waits/ ) ( https://www.selenium.dev/documentation/en/webdriver/waits/ )

在此处输入图片说明

Could you share the full code with us?你能和我们分享完整的代码吗? It seems that you forget to delete the 30 sec wait from somewhere.您似乎忘记从某处删除 30 秒等待。 – peetya – 皮蒂亚

This was super straight to the point, I had the same issue but it was just because I declared implicit wait for 30 seconds when launching the browser, didn't think to look back at it because it seemed "trivial".这非常直截了当,我遇到了同样的问题,但这只是因为我在启动浏览器时声明了隐式等待 30 秒,没有想到回头看它,因为它看起来“微不足道”。 Thanks so much for pointing this out peetya.非常感谢您指出这一点 peetya。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM