一、隐形加载等待:
file:///C:/Users/leixiaoj/Desktop/test.html 该页面负责创建一个div
<html> <head><title>Set Timeout</title><style>.red_box {background-color: red; width = 20%; height:100px; border: none;}</style><script>function show_div(){setTimeout("create_div()", 5000);}function create_div(){d = document.createElement('div');d.className = "red_box";document.body.appendChild(d);}</script> </head> <body><button id = "b" onclick = "show_div()">click</button> </body> </html>
等候10s后再修改div
//隐式等待
public static void main(String[] args) throws InterruptedException {WebDriver dr = new FirefoxDriver();//设置10 秒dr.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);String url = "file:///C:/Users/leixiaojiang/Desktop/test.html";dr.get(url);dr.findElement(By.id("b")).click();WebElement element =dr.findElement(By.cssSelector(".red_box"));((JavascriptExecutor)dr).executeScript("arguments[0].style.border =\"5px solid yellow\"",element); }
//显式等待WebDriver dr = new FirefoxDriver();String url = "file:///C:/Users/leixiaojiang/Desktop/test.html";dr.get(url);WebDriverWait wait = new WebDriverWait(dr,10);wait.until(new ExpectedCondition<WebElement>(){@Overridepublic WebElement apply(WebDriver d) {return d.findElement(By.id("b"));}}).click();//上面的点击操作5s后才有red_box这个类出现WebDriverWait wait1 = new WebDriverWait(dr,10);WebElement element =dr.findElement(By.cssSelector(".red_box"));((JavascriptExecutor)dr).executeScript("arguments[0].style.border =\"5px solid yellow\"",element);