WebDriver Waits Examples

We can use WebDriverWait class in many different cases. When ever we need to perform any operation on element, we can use webdriver wait to check if the element is Present or visible or enabled or disabled or Clickable etc.

We will look into different examples for all the above scenarios:

isElementPresent:

Below is the syntax to check for the element presence using WebDriverWait. Here we need to pass locator and wait time as the parameters to the below method. Here it is checking that an element is present on the DOM of a page or not. That does not necessarily mean that the element is visible. ExpectedConditions will return true once the element is found in the DOM.

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 

We should use presenceOfElementLocated when we don't care about the element visible or not, we just need to know if it's on the page.

We can also use the below syntax which is used to check if the element is present or not. We can return true ONLY when the size of elements is greater than Zero. That mean there exists atleast one element.

WebElement element = driver.findElements(By.cssSelector(""));
element.size()>0;

isElementClickable

Below is the syntax for checking an element is visible and enabled such that we can click on the element. We need to pass wait time and the locator as parameters.

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.elementToBeClickable(locator));

isElementVisible

Below is the syntax to check if the element is present on the DOM of a page and visible. Visibility means that the element is not just displayed but also should also has a height and width that is greater than 0.

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

We can also use the below to check the element to be visible by WebElement.

WebDriverWait wait = new WebDriverWait(driver, waitTime);
 wait..until(ExpectedConditions.visibilityOf(element));

We can also use the below to check all elements present on the web page are visible. We need to pass list of WebElements.

List<WebElement> linkElements = driver.findelements(By.cssSelector('#linkhello'));
WebDriverWait wait = new WebDriverWait(driver, waitTime);
 wait..until(ExpectedConditions.visibilityOfAllElements(linkElements));

isElementInVisible

Below is the syntax which is used for checking that an element is either invisible or not present on the DOM.

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator)); 

isElementEnabled

Below is the syntax which is used to check if the element is enabled or not

WebElement element = driver.findElement(By.id(""));
element.isEnabled();

isElementDisplayed

Below is the syntax which is used to check if the element is displayed or not. It returns false when the element is not present in DOM.

WebElement element = driver.findElement(By.id(""));
element.isDisplayed();

Wait for invisibility of element

Below is the syntax which is used to check for the Invisibility of element with text

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementWithText(by));

Wait for invisibility of element with Text

Below is the syntax which is used for checking that an element with text is either invisible or not present on the DOM.

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementWithText(by, strText));
Selenium Tutorials: 

Comments

Great Piece of Information. I really appreciate it.

It would also be great if you provide an URL where we can try all this commands i.e a website with two drop downs which are dependent on each other.

Hi,,
Could pls someone clarify these doubts:

One webdriver script that i wrote is failing sometimes(not all the times i run).

I feel the webdriver is passing the control to the next line of execution even before the browser loads the page completely. And since the element in that next line of code is not available that time,, the execution stops and the browser closes before executing next 7-8 steps.

I am using Page Object Model (@FindBy) annotations.
I wanted to know if @FindBy implicitly/internally invokes implicit wait?? Or do i need to include wait in some other place/way?

Add new comment