Selenium WebDriverWait throwing exception

Viewed 2800

I'm trying to wait until a element is present before continuing, however with the method I'm currently using it's throwing element does not exist, even before timeout has ended.

Example usage:

 using OpenQA.Selenium;
 using OpenQA.Selenium.Chrome;
 using OpenQA.Selenium.Support.UI;

 private static WebDriverWait wait = new WebDriverWait(driver, System.TimeSpan.FromSeconds(30)); // inside parent class (not method)

 //public static void Method1()

 wait.Until(driver => driver.FindElement(By.CssSelector("[foo=bar]")));

which should then wait 30 seconds or until element is present, but it instantly throws element not found exception...

1 Answers

Try using ExpectedConditions class in general and ElementExists() function in particular, suggested code:

https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_ExpectedConditions_ElementExists.htm

Example code:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element  = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("[foo=bar]")));

Example output:

enter image description here

More information:

Related