package com.test.elementwait;app
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;ui
public class ExplicitWait {firefox
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();orm
//標題是否是「百度一下,你就知道」
new WebDriverWait(driver,5).until(ExpectedConditions.titleIs("百度一下,你就知道"));
//標題是否是包含「百度一下」
new WebDriverWait(driver,5).until(ExpectedConditions.titleContains("百度一下"));
//判斷該元素是否被加載在DOM中,並不表明該元素必定可見
new WebDriverWait(driver,5).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='kw']")));
//判斷元素(定位後)是否可見
new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@id='kw']"))));
//判斷元素是否可見(非隱藏,而且元素的寬和高都不等以0)
new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='kw']")));
//只要存在一個就是true
ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id='kw']"));
//元素中的text是否包含語氣的字符串
ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@id='kw']"), "百度一下");
//元素的value屬性中是否包含語氣的字符串
ExpectedConditions.textToBePresentInElementValue(By.xpath("//*[@id='kw']"), "***");
//判斷該表單是否能夠切過去,能夠就切過去並返回true,不然放回false
ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("**"));
//判斷某個元素是否不存在於DOM或不可見
ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='kw']"));
//判斷元素是否能夠點擊
ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='kw']"));
//等到一個元素從DOM中移除
ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//*[@id='kw']")));
//判斷某個元素是否被選中,通常用在下拉列表
ExpectedConditions.elementToBeSelected(By.xpath("//*[@id='kw']"));
//判斷某個元素的選中狀態是否符合預期
ExpectedConditions.elementSelectionStateToBe(By.xpath("//*[@id='kw']"), true);
//判斷某個元素(已定位)的選中狀態是否符合預期
ExpectedConditions.elementSelectionStateToBe(driver.findElement(By.xpath("//*[@id='kw']")), false);
//判斷頁面中是否存在alert
new WebDriverWait(driver,5).until(ExpectedConditions.alertIsPresent());
//--------------------自定義判斷條件-----------------------------
WebDriverWait wait = new WebDriverWait(driver, 3);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return !driver.findElement(By.xpath("//*[@id='kw']")).getAttribute("class").contains("x-form-invalid-field");
}
});
}ci
}element