1

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.

alecxe
476k127 gold badges1.1k silver badges1.2k bronze badges
asked Nov 1, 2015 at 20:17

1 Answer 1

1

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).

answered Nov 1, 2015 at 20:19
Sign up to request clarification or add additional context in comments.

2 Comments

This is still not working for me. Do I have to use any special using " ..." ?
@omnibored the explicit wait should work for you - you just need to carefully pick up an element/condition to wait for. Not sure what to add more here.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.