版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsj880921/article/details/43056069
不多说,直接上代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2015/1/23.
*/
public class TaquTools {
<pre name="code" class="java"> /**
* 等待元素加载
*
* @param driver driver
* @param by 定位方式
* @param waitTime 等待时间
*/
public static void waitForVisible(WebDriver driver, final By by, int waitTime) {
WebDriverWait wait = new WebDriverWait(driver, waitTime);
for (int attempt = 0; attempt < waitTime; attempt++) {
try {
driver.findElement(by);
break;
} catch (Exception e) {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
}
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
使用:
TaquTools.waitForVisible(driver, By.id("tvEntranceOne"), 5);// 等待某元素加载,5S未能加载变为超时
同样这个方法在WebDriver也是可用