1、隱式等待css
以https://www.yahoo.com/的登陸爲例,須要在用戶名輸入框中輸入「test」java
咱們先用未設置隱式等待的代碼看看執行效果:api
1 package waittyps; 2 3 import org.junit.jupiter.api.AfterEach; 4 import org.junit.jupiter.api.BeforeEach; 5 import org.junit.jupiter.api.Test; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.ie.InternetExplorerDriver; 9 10 class ImplicitWaitDemo { 11 private WebDriver driver; 12 private String url; 13 14 @BeforeEach 15 void setUp() throws Exception { 16 driver = new InternetExplorerDriver(); 17 url = "https://www.yahoo.com/"; 18 driver.manage().window().maximize(); 19 } 20 21 @Test 22 void test() { 23 driver.get(url); 24 driver.findElement(By.id("uh-signin")).click(); 25 driver.findElement(By.cssSelector("#login-username")).sendKeys("test"); 26 } 27 28 @AfterEach 29 void tearDown() throws Exception { 30 Thread.sleep(2000); 31 driver.quit(); 32 } 33 }
執行結果:執行失敗,提示未定位到輸入框元素(元素路徑是沒有問題的,由於未設置等待時間,因此沒有找到「login-username」,致使控制檯直接報錯)ui
如今咱們設置隱式等待時間3秒,而後運行代碼:url
1 package waittyps; 2 3 import java.util.concurrent.TimeUnit; 4 5 import org.junit.jupiter.api.AfterEach; 6 import org.junit.jupiter.api.BeforeEach; 7 import org.junit.jupiter.api.Test; 8 import org.openqa.selenium.By; 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.ie.InternetExplorerDriver; 11 12 class ImplicitWaitDemo { 13 private WebDriver driver; 14 private String url; 15 16 @BeforeEach 17 void setUp() throws Exception { 18 driver = new InternetExplorerDriver(); 19 url = "https://www.yahoo.com/"; 20 driver.manage().window().maximize(); 21 // 隱式等待3秒 22 driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 23 } 24 25 @Test 26 void test() { 27 driver.get(url); 28 driver.findElement(By.id("uh-signin")).click(); 29 driver.findElement(By.cssSelector("#login-username")).sendKeys("test"); 30 } 31 32 @AfterEach 33 void tearDown() throws Exception { 34 Thread.sleep(2000); 35 driver.quit(); 36 } 37 }
運行結果:成功spa
2、顯式等待3d
1 package waittyps; 2 3 import org.junit.jupiter.api.AfterEach; 4 import org.junit.jupiter.api.BeforeEach; 5 import org.junit.jupiter.api.Test; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.WebElement; 9 import org.openqa.selenium.ie.InternetExplorerDriver; 10 import org.openqa.selenium.support.ui.ExpectedConditions; 11 import org.openqa.selenium.support.ui.WebDriverWait; 12 13 class ImplicitWaitDemo { 14 private WebDriver driver; 15 private String url; 16 17 @BeforeEach 18 void setUp() throws Exception { 19 driver = new InternetExplorerDriver(); 20 url = "https://www.yahoo.com/"; 21 driver.manage().window().maximize(); 22 // 隱式等待3秒 23 // driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 24 } 25 26 @Test 27 void test() { 28 driver.get(url); 29 WebElement el = driver.findElement(By.id("uh-signin")); 30 el.click(); 31 // 實例化WebDriverWait對象,括號中內容表示(做用於誰,顯式等待時長) 32 WebDriverWait wait = new WebDriverWait(driver,2); 33 // 用emailField接受查找到的元素,visibilityOfElementLocated表示元素可見後再執行輸入操做 34 // 使用顯式等待3秒,直到#login-username元素可用,若是在3秒內該元素可用就執行後面的操做,不然超過3秒直接拋出異常 35 WebElement emailField = wait.until( 36 ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#login-username"))); 37 emailField.sendKeys("test"); 38 // driver.findElement(By.cssSelector("#login-username")).sendKeys("test"); 39 } 40 41 @AfterEach 42 void tearDown() throws Exception { 43 Thread.sleep(2000); 44 driver.quit(); 45 } 46 }