简体   繁体   English

Selenium C#wait.until(expectedconditions)…函数无法在屏幕上找到对象/元素

[英]Selenium c# wait.until(expectedconditions)… function fails to find objects/elements on screen

I am new to using selenium Webdriver and writing c# . 我不Webdriver使用Webdriver和编写c# I was using lots of thread.sleep() commands in my script to make sure Selenium didn't try and click too quick. 我在脚本中使用了许多thread.sleep()命令,以确保Selenium不会尝试并单击得太快。 Upon lots of research I found this is frowned upon and there are "better" ways to do it, so I started to use:- 经过大量研究,我发现这种方法不被接受,并且有“更好”的方法,所以我开始使用:

wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("FieldId")));

Wait is defined in another class as:- 等待在另一个类中定义为:-

wait = new WebDriverWait(driver, new TimeSpan(0, 0, 0, 10));

I can only get this to work 2/10 times of running the script without getting : 我只能使它能够运行脚本的2/10次,而不会得到:

No such element exception: cannot locate element...... 没有这样的元素异常:无法找到元素……

I have tons of examples of this problem now I have stopped using thread.sleep . 我有很多关于此问题的示例,现在我已经停止使用thread.sleep After lots of research I always come to find this is the advised way but it seems totally unreliable. 经过大量研究,我总是发现这是建议的方法,但似乎完全不可靠。 Have I missed something? 我错过了什么吗? The element is always there after 2 seconds and the error occurs before 10sec has passed. 该元素始终在2秒后出现,并且错误在10秒过去之前发生。 I have read at least 10 other posts which sound similar but none of them have a solution which works...except Thread.Sleep(5000) ! 我已经阅读了至少10篇听起来类似的文章,但没有一个有Thread.Sleep(5000)的解决方案... Thread.Sleep(5000)除外!

Try adding the NoSuchElementException type to the IgnoreExceptionTypes of the waiter after you create it. 创建后,请尝试将NoSuchElementException类型添加到服务员的IgnoreExceptionTypes中。

wait = new WebDriverWait(driver, new TimeSpan(0, 0, 0, 10));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("FieldId")));

In general, if the waiter is returning before the timeout you indicate is expired, it's because an exception occurred that is not currently ignored. 通常,如果服务员在您指定的超时到期之前返回,那是因为发生了异常,当前未将其忽略。

The WebDriverWait class is derived from the DefaultWait class. WebDriverWait类是从DefaultWait类派生的。 Both of which you can review on github which I found really helpful in understanding how to use it. 两者都可以在github上查看,我发现它对理解如何使用它很有帮助。

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

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