WebDriver的等待方式

时间:2023-03-09 20:11:45
WebDriver的等待方式
/*
* 1.线程休眠
* 2.隐式等待
* 3.显示等待
*
*/
package com.sfwork; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; public class WebWait { public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//设置WebDriver的路径
// System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\"
// + "Application\\chromedriver.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\"
+ "Mozilla Firefox\\geckodriver.exe");
//创建webdriver对象,控制浏览器
// WebDriver driver = new ChromeDriver();
WebDriver driver = new FirefoxDriver();
//设置打开浏览器的网址
driver.navigate().to("http://www.baidu.com");
//设置浏览器最大化
driver.manage().window().maximize();
//线程休眠
// Thread.sleep(3000);
//隐式等待,全局的
// driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
// driver.findElement(By.id("22"));
//显示等待,针对特定的一个元素
WebDriverWait wait = new WebDriverWait(driver,15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("22")));
//关闭webdriver
driver.close(); } }