[英](C# Selenium) How to wait until an element is present?
I am completely new to Selenium, started using it yesterday and I am pretty much done with the first part of my project and I really love the way its going. 我对Selenium完全陌生,昨天就开始使用它,并且我已经完成了项目的第一部分,而且我真的很喜欢Selenium的运行方式。
Altho I am having one problem, currently I am using Thread.Sleep for pauses until elements get present. 我遇到了一个问题,目前我正在使用Thread.Sleep暂停直到元素出现。 There are some animations going on or just slow loading at times, how can I make it wait until the element is present and then interact with it?
有时会出现一些动画,或者只是加载缓慢,我如何才能让它等到元素出现后再与之交互?
For example: 例如:
LoginPageElements loginPage = new LoginPageElements();
loginPage.continueLink.Click();
Thread.Sleep(5000); //here it has to wait until the next page loads
ClickDailyBonusPopUp();
Driver.driver.Navigate().GoToUrl(.....);
Thread.Sleep(2000); //here it has to wait until a login form pops up
LoginFormElements loginForm = new LoginFormElements();
loginForm.userPasswordLogin.Click();
Thread.Sleep(2000); //here it has to wait until a different login form pops up
You need to use WebDriverWait class. 您需要使用WebDriverWait类。 Here is really good solution for your problem.
这确实是解决您问题的好方法。
If your application uses jQuery, I would make use of Selenium's IJavaScriptExecutor
to do something like this: 如果您的应用程序使用jQuery,我将利用Selenium的
IJavaScriptExecutor
进行以下操作:
public void WaitForAjax()
{
if ((bool)((IJavaScriptExecutor)WebDriver).("return window.jQuery != undefined"))
{
while (true)
{
var ajaxIsComplete = (bool)((IJavaScriptExecutor)WebDriver).ExecuteScript("return jQuery.active == 0");
if (ajaxIsComplete)
break;
Thread.Sleep(100);
}
}
}
(More on jQuery.active
here .) (有关
jQuery.active
更多信息,请点击此处 。)
The main idea is to find some condition or element to poll for/wait on that is always present when the page isn't ready. 主要思想是找到一些条件或元素以进行轮询/等待,以便在页面未准备好时始终存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.