Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Last active Sep 12, 2018
Embed
What would you like to do?
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)
@MSithara

This comment has been minimized.

Copy link

@MSithara MSithara commented Jan 17, 2018

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");

}
}

@diljeetranaut

This comment has been minimized.

Copy link

@diljeetranaut diljeetranaut commented Jul 5, 2018

Hi,

try this code

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

@nikolay-advolodkin

This comment has been minimized.

Copy link

@nikolay-advolodkin nikolay-advolodkin commented Sep 12, 2018

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