selenium中等待的四種方式

selenium中等待的四種方式,本文用java開發語言編寫

1、強制等待

      強制等待使用方法Thread.sleep(int sleeptime),此方法會把當前的driver進程暫停一段時間,然後在執行接下來的操作,這個方法的缺點就是,你不能確定元素到底多久加載出來,如果你的sleeptimes是10秒,但是元素2秒就加載出來了,那麼進程上還會繼續等待8秒,造成時間浪費。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class KeywordSleepTime {

	public static void main(String[] args) throws InterruptedException {
		
		//啓動firefox瀏覽器,打開百度頁面,然後等待5秒,等待百度頁面全部加載完成
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.get("http://www.baidu.com");
		Thread.sleep(5000);
		driver.quit();
	}
}

2、頁面等待

       有時候我們打開一個網頁,網頁加載速度比較慢,但是呢又想等網頁完全加載完畢了在執行操作,那麼就可以用
 pageLoadTimeout(pageLoadTime,TimeUnit.SECONDS)這個方法,如pageLoadTime時間設置爲5秒,如果在5秒內

網頁還沒有全部加載出來就會報錯,如果小於5秒就全部加載出來了,剩下的時間將不再等待.

 

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class KeywordPageLoadTime {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		
		//設置等待時間爲5秒,如果5秒百度頁面沒有全部加載出來,就會報錯,如果小於5秒就全部加載出來了,剩下的時間將不再等待,繼續下一步操作
		driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
		driver.get("http://www.baidu.com");
	}
}

3、隱式等待

        隱式等待(implicit),方法implicitlyWait(long time, TimeUnit.SECONDS),如:long time設置的時間爲10秒,如果10秒之內,
 特定元素沒有加載完成,則拋出異常,如果小於10秒之內特定元素加載完成,剩下的時間將不再等待。   
      隱性等待對整個driver都有作用,只需要設置一次

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class KeywordImplicitlyWait {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		
		//設置等待時間爲5秒,如果5秒百度頁面沒有全部加載出來,就會報錯,如果小於5秒就全部加載出來了,剩下的時間將不再等待,繼續下一步操作
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("http://www.baidu.com");		
	}
}

4、顯式等待

           顯式等待(explicit),顯式等待情況下,只有特定條件觸發後,也就是說明確的等到界面某元素出現或者某可點擊

等條件爲止,WebDriver 纔會繼續執行後續操作,等不到,就一直等,除非在規定的時間之內都沒找到,那麼

就拋出org.openqa.selenium.TimeoutExceptionn異常。

        方法一:

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 KeywordWebdriverWait1 {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.get("https://www.baidu.com/");
		
		/*
		 * 等待時間爲10秒,WebDriverWait默認每500ms就調用一次ExpectedCondition直到定位到“百度一下”按扭,
		 * 如果 10秒內“百度一下”按扭顯示出來,則繼續下一步,如果超過10秒沒有顯示出來,那麼則until()會
		 * 拋出org.openqa.selenium.TimeoutExceptionn異常
		 */
		WebDriverWait  wait = new WebDriverWait(driver,10);
		WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("su")));
		boolean display = element.isDisplayed();
		System.out.println(display);
	}
}

方法二:

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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class KeywordWebdriverWait2 {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.get("https://www.baidu.com/");
		
		/*
		 * 等待時間爲10秒,WebDriverWait默認每500ms就調用一次ExpectedCondition直到定位到“百度一下”按扭,
		 * 如果 10秒內“百度一下”按扭顯示出來,則繼續下一步,如果超過10秒沒有顯示出來,那麼則until()會
		 * 拋出org.openqa.selenium.TimeoutExceptionn異常
		 */	
		WebDriverWait  wait = new WebDriverWait(driver,10);
		try {
			WebElement element = wait.until(new ExpectedCondition<WebElement>(){
				//重寫方法
				public WebElement apply(WebDriver driver) {
					return driver.findElement(By.id("su"));
				}	
			});
			boolean display = element.isDisplayed();
			System.out.println(display);
			driver.quit();						
		} catch (Exception e) {
			System.out.println("未找到元素");
		}

	}

}

 

顯式等待使用ExpectedConditions類中自帶方法, 可以進行顯式等待的判斷,常用的判斷條件如下:

(1)頁面元素是否在頁面上可用和可被單擊:elementToBeClickable(By locator)

(2)頁面元素處於被選中狀態:elementToBeSelected(WebElement element)

(3)頁面元素在頁面中存在:presenceOfElementLocated(By locator)

(4)在頁面元素中是否包含特定的文本:textToBePresentInElement(By locator)

(5)頁面元素值:textToBePresentInElementValue(By locator, java.lang.String text)

(6)標題 (title):titleContains(java.lang.String title)       

顯式等待常跟以下三種方法一起使用,用來判斷元素

        (1)、 isEnable()      檢查元素是否被啓用
        (2)、 isSelected()   檢查元素是否被選中
        (3)、 isDisplay()      檢查元素是否可見

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章