Home > Net >  Waiting in Selenium and C#
Waiting in Selenium and C#

Time:06-07

When using Selenium/C# there are times where one cannot be sure if elements will be returned on not. This can be due to no records found, random date selection, type of records returned etc.

So in such cases waiting for an element to exist/visible/clickable is not the best method. Also I do not like using Thread.Sleep and at times waiting for a page to complete loading while Angular is still doing its stuff does not always work.

So in such circumstances is there a safe wait to employ a wait method for say 2 seconds without Thread.Sleep?

Any thoughts please?

CodePudding user response:

It's worth exploring the WebDriverWait approach. The boolean expression you provide can include multiple conditions so if you want to be checking UI elements that may or may not exist, you can cover these scenarios.

Here's just a little example.

new WebDriverWait(driver, TimeSpan.FromSeconds(10))
    .Until(driver => driver.FindElementById("your-id").Displayed ||
                     driver.FindElementById("other-id").Displayed));

Though this may be an application user experience problem. There should be something in the UI to indicate the state of a page and you can accommodate for that in your wait condition with a variety of condition checks for the expected UI state.

  • Related