简体   繁体   English

C# - Selenium - 如何等待页面完全加载

[英]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.

相关问题 C#Web请求-如何等待页面及其所有后续jquery / ajax调用完成并且页面已完全加载 - C# web request - how to wait until page and all of its subsequent jquery/ajax calls have completed and page has fully loaded 等待直到使用C#中的SHDocVw中的InternetExplorer对象完全加载页面? - Wait until page is fully loaded using InternetExplorer Object from SHDocVw in C#? 与PhantomJs的硒等到页面满载? - selenium with PhantomJs wait till page fully loaded? 如何等到场景完全加载? - How to wait until a scene is fully loaded? 如何在无头模式下使用 Puppeteer 抓取 HTML 之前加载 web 页面? (C#) - How to wait until web page is loaded before scraping HTML using Puppeteer in headless mode? (C#) C#Selenium WebDriver等待一些元素完全加载后再继续 - C# Selenium WebDriver wait for some elements to be fully loaded before going on C#WebBrowser-如何等待直到加载DOM元素? - C# WebBrowser - How to wait until DOM Element is loaded? (C#Selenium)如何等待元素出现? - (C# Selenium) How to wait until an element is present? 如何等到元素显示? selenium 网络驱动程序 C# - How to wait until element is shown? selenium webdriver C# 如何等待直到通过C#中的硒自动化测试验证了登录名 - how to wait until login is verified with selenium automation testing in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM