selenium wait.until

这4种方法,都可以实现等待元素出现

package com.test_;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test {
	/**
	 * @param args
	 */
	private static final String urlString = "http://www.baidu.com";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.setProperty("webdriver.chrome.driver", "D:\\BaiduYunDownload\\chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		driver.get(urlString);
		driver.findElement(By.linkText("登录")).click();
		
		
		new WebDriverWait(driver, 10).until(new ExpectedCondition() {
	        @Override
	        public Boolean apply(WebDriver driver) {
	          return driver.findElement(By.name("userName")).isDisplayed();
	        }
	      });
		
		
//		WebDriverWait wait = new WebDriverWait(driver, 10);
		
//		wait.until(new Function() {
//			@Override
//			public Boolean apply(WebDriver driver) {
//				// TODO Auto-generated method stub
//				return null;
//			} 
//		});
		
		
		//这2中方法都行,都可以等待
//		wait.until(new Predicate() {
//			@Override
//			public boolean apply(WebDriver driver) {
//				// TODO Auto-generated method stub
//				return driver.findElement(By.name("userName")).isDisplayed();
//			}
//		});
//		
				
		WebElement element = driver.findElement(By.name("userName"));
		
		element.clear();
		element.sendKeys("123");
        	
	}
	
//	 public static Function isDisplay(){
//	 	 
//		 return new Function() {
//			 @Override
//			public Boolean apply(WebDriver driver) {
//				// TODO Auto-generated method stub
//				return driver.findElement(By.name("userName")).isDisplayed();
//			}
//			 
//		};
//		
//		 
//	 }
	

}


你可能感兴趣的:(selenium)