Tuesday, December 16, 2014


If a particular web element takes more than a minute to load then you may want to wait for the element to load properly and then go to the next line of code. In selenium, it is possible with Explicit Wait.





Selenium Explicit Wait Example


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Training {
 static WebDriver driver;

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

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www. w3schools. com/js/tryit. asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  WebElement element1 = (new WebDriverWait(driver, 10))
    .until(ExpectedConditions.elementToBeClickable(By
      .tagName("button")));
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();
 }

}