How to use clickandwait in Selenium Webdriver using Java?


We can click and wait in Selenium. This can be achieved by the synchronization concept. We shall use the explicit wait condition and wait for an element to be clickable prior to the next step.

The explicit wait waits for a specified amount of time before throwing an exception. To verify if an element is clickable, we can use the expected condition elementToBeClickable.

Example

Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ElementClickableWait{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      // identify element and click()
      WebElement l=driver.findElement(By.className("mui-btn"));
      l.click();
      // explicit wait condition
      WebDriverWait w = new WebDriverWait(driver,3);
      // elementToBeClickable condition
      w.until(ExpectedConditions
      .elementToBeClickable (By.className("s-buy")));
      // get page title of next page
      System.out.println("Current page title:" + driver.getTitle());
      driver.quit();
   }
}

Output

Updated on: 28-Dec-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements