When working with multiple elements retrieved as a list of web elements, sometimes we need to wait for a specific element in that list to be visible before performing actions.
Here are two common approaches to achieve this.
Method 1: Using xpath and WebDriverWait
In this method, we identify the required element by xpath and wait using WebDriverWait
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import java.time.Duration;
import java.util.List;
public class SampleTest {
WebDriver driver = null;
@Test
public void waitUsingXPath() {
driver = new ChromeDriver();
driver.get("https://example.com");
WebElement elem = driver.findElement(By.xpath("//a[1]"));
// Wait for element to be visible
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(ExpectedConditions.visibilityOf(elem));
System.out.println("Element is now visible: " + elem.getText());
}
}
Method 2: Using WebDriverWait with ExpectedConditions
In this method, we identify the required element by its index in the list and wait until it becomes visible.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import java.time.Duration;
import java.util.List;
public class SampleTest {
WebDriver driver = null;
@Test
public void waitUsingListIndex() {
driver = new ChromeDriver();
driver.get("https://example.com");
List<WebElement> list = driver.findElements(By.tagName("a"));
// Get the first element
WebElement elem = list.get(0);
// Wait for element to be visible
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(ExpectedConditions.visibilityOf(elem));
System.out.println("Element is now visible: " + elem.getText());
}
}
Method 3: Using a Custom Wait Method with Thread.sleep()
Here we create our own wait loop to repeatedly check if the element is displayed, pausing between checks.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.util.List;
public class SampleTest {
WebDriver driver = null;
@Test
public void waitUsingCustomLoop() throws InterruptedException {
driver = new ChromeDriver();
driver.get("https://example.com");
List<WebElement> list = driver.findElements(By.tagName("a"));
// Get the first element
WebElement elem = list.get(0);
// Custom wait method
waitForElement(elem, 60);
System.out.println("Element is now visible: " + elem.getText());
}
public void waitForElement(WebElement elem, int maxTime) throws InterruptedException {
int elapsedTime = 0;
while (elapsedTime < maxTime) {
if (elem.isDisplayed()) {
break;
}
Thread.sleep(5000); // Wait for 5 seconds
elapsedTime += 5;
}
}
}