How can I ask the Selenium-WebDriver to wait for few seconds in C#?

How can I ask the Selenium-WebDriver to wait for few seconds in C#?

There are three ways of waiting for elements: explicit wait, implicit wait and fluent wait.

Implicit wait will simply wait for a specified amount of time, returning the element if it is found when the timer runs out.

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
IWebElement query = driver.FindElement(By.XPath("//h1[text()='Moon']"));

 

Explicit wait waits until either a certain condition is met or the timeout has been reached before attempting to return an element.

 

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(driver=>driver.FindElement(By.XPath("//h1[text()='Moon']")));

 

Fluent wait is similar to explicit wait except it is more flexible in that the timing of when the page is checked can be altered and also can be told to ignore certain errors while it is waiting.

 

 

DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver);
fluentWait.Timeout = TimeSpan.FromSeconds(5);
fluentWait.PollingInterval = TimeSpan.FromMilliseconds(5000);
fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
fluentWait.Message = "Element searched for but not found.";

IWebElement searchResult = fluentWait.Until(driver => driver.FindElement(By.XPath("//h1[text()='Moon']")));

 

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.