Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Selenium Web Driver Wait For Element Visibility

In Selenium test automation, sometimes, the test application should wait until an element is visible. Here is the code sample for it.  

In this sample, Selenium web driver will wait for an element visibility until the timeout seconds elapse. If timeOutInseconds is up, the code snippet will display a message on the console. 

Code Snippet
  1. private void WaitForElementVisible(By by, int timeOutInSeconds)
  2.         {
  3.             Stopwatch stopwatch = new Stopwatch();
  4.             stopwatch.Start();
  5.             try
  6.             {
  7.                 var wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(timeOutInSeconds));
  8.                 wait.Until(ExpectedConditions.ElementIsVisible(by));
  9.             }
  10.             catch (Exception)
  11.             {
  12.                 Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed.Seconds);
  13.             }
  14.         finally
  15.             {
  16.                 stopwatch.Stop();
  17.             }
  18.         }