標籤:val org expected pen select logs main 百度一下 enc
顯式等待可以使用selenium預置的判斷方法,也可以使用自訂的方法。
package com.test.elementwait;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;public class ExplicitWait { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); //標題是不是“百度一下,你就知道” 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"); } }); }}
註:
1.除了以上內容,selenium還提供了很多預置的判斷方法。
2.這些判斷方法,在超出時間限制時就會拋出異常。
selenium測試(Java)-- 顯式等待(九)