Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Last active September 12, 2018 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadvolod/b3a4f1c44f229a18037d48290df4994e to your computer and use it in GitHub Desktop.
Save nadvolod/b3a4f1c44f229a18037d48290df4994e to your computer and use it in GitHub Desktop.
This is how you do an explicit wait in Selenium WebDriver
//EXAMPLE 1 - This is the most convenient method provided to us by the Webdriver API
//ExpectedConditions class provides us many different options for locating an element
var wait = new WebDriverWait(_driver,TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("elementId")));
//EXAMPLE 2 - This is a wait that dynamically checks for the presence of an element for a maximum amount of time, a bit burdensome
//because we had to write it
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id("someDynamicElement"));
});
//EXAMPLE 2 - This is the worst kind of wait and should almost never be used
Thread.Sleep(10000)
@nikolay-advolodkin
Copy link

Hi,
I wrote a simple code to launch a website on firefoxbrowser and I used the //EXAMPLE 3 for waiting the page to load for timeout issue.I got the following error "WebDriverWait cannot be resolved to a type" "TimeSpan cannot be resolved" "The method ElementIsVisible(By) is undefined for the type ExpectedCondition" after running the following script.

package mynewpackage;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;

public class Var {
public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");  
WebDriver driver = new FirefoxDriver();
Var wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
wait.Until(ExpectedCondition.ElementIsVisible(ById.id("elementId")));
driver.get("http://www.toolsqa.com");

System.out.println("Successfully opened the website");

}
}

My code is in C#, yours is in Java. Your conventions are probably a bit different in Java. Check Explicit Waits in Java @MSithara

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment