omnibored
omnibored

Reputation: 23

How to delay or wait in c# for selenium script?

I am making a script to test a website using C# and selenium. I am having an issue where a page doesn't load all the elements instantly so my code produces an error exception that the element doesn't exist. Is there a way to make my code pause and wait for all the elements to load, or set a fixed time to wait?

I have tried some things like:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));

But I don't think its working.

Upvotes: 1

Views: 3674

Answers (1)

alecxe
alecxe

Reputation: 473863

Fixed time to wait would not be reliable at all. Instead, you are asking about an Explicit Wait functionality - explicitly waiting for some expected condition to be met - for instance, waiting for a specific element to become present or visible. Example:

IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3))
IWebElement element = wait.Until(driver => driver.FindElement(By.Name("q")));

In this example, selenium webdriver would wait up to 3 seconds for an element with name="q" to be present checking its presence every 500 ms (by default).

Upvotes: 1

Related Questions