[英]C# - Selenium - How to wait until page is fully loaded
Due to slow synchronization on my testing website, I need to wait the page until is fully loaded.由于我的测试网站同步缓慢,我需要等待页面完全加载。 I have tried several elements waits like:
我尝试了几个元素等待,如:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(50));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//input[@placeholder='First Name']"))).SendKeys("FirstName");
Also I have attempt with JavaScript methods like:我也尝试过使用 JavaScript 方法,例如:
public static void WaitForLoadOriginal(IWebDriver driver)
{
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
int timeoutSec = 15;
WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, timeoutSec));
wait.Until(wd => js.ExecuteScript("return document.readyState").ToString() == "complete");
}
This is the code that i try to select这是我尝试选择的代码
<select name="question" id="question1" type="text" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" ng-required="true"
and this is the exception that i got:这是我得到的例外:
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.Support.dll but was not handled in user code $exception {"Cannot locate option with index: 1"} OpenQA.Selenium.NoSuchElementException 'OpenQA.Selenium.NoSuchElementException' 类型的异常发生在 WebDriver.Support.dll 中,但没有在用户代码 $exception {"Cannot locate option with index: 1"} OpenQA.Selenium.NoSuchElementException 中处理
If I put Thread.Sleep(20000)
in, it works fine.如果我把
Thread.Sleep(20000)
放进去,它工作正常。 But I don't want to use static waits.但我不想使用静态等待。 Either I am not using the above methods properly, or they are not working.
要么我没有正确使用上述方法,要么它们不起作用。 Any help would be appreciated.
任何帮助将不胜感激。
As per your code block as you are trying to invoke SendKeys() along WebDriverWait instead of ExpectedConditions clause as ElementExists you sould use the clause ElementToBeClickable as follows :根据您的代码块,当您尝试沿着WebDriverWait而不是ExpectedConditions子句调用SendKeys()作为ElementExists 时,您应该使用ElementToBeClickable子句,如下所示:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@placeholder='First Name']"))).SendKeys("FirstName");
To perform waiting for page to load, this piece of code works with me fine.为了执行等待页面加载,这段代码对我来说很好用。
protected void WaitForPageLoad()
{
wait.Until(driver=>((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
}
Thank you谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.