WebDriverWait使用如下
(new WebDriverWait(driver,6))
.until(ExpectedConditions.presenceOfElementLocated(By by)).click();
ExpectedConditions的一些方法:
1.titleIs( String title)
判断当前页面的title是否精确等于预期
2.titleContains( String title)
判断当前页面的title是否包含预期字符串
3.urlToBe( String url)
判断当前页面的url是否精确等于预期
4.urlContains( String fraction)
判断当前页面的url是否包含预期字符串
5.urlMatches( String regex)
6.presenceOfElementLocated( By locator)
判断页面元素是否出现,在等待时间内没有出现就抛出异常
7.visibilityOfElementLocated( By locator)
判断元素是否出现
8.alertIsPresent()
9.elementToBeSelected
元素是否可以被点击
10.elementToBeClickable
复选框是否可以被点击
11.textToBePresentInElement
判断字样是否在元素中显示
WebDriverWait wait = new WebDriverWait( Driver.getCurrentDriver(),10);
//调用ExpectedConditions的titleContains方法判断页面title属性是否包含"xueqiu"字段
wait.until(ExpectedConditions.titleContains("xueqiu"));
wait.until(ExpectedConditions.titleIs("雪球"));
WebElement select = Driver.getCurrentDriver().findElement(By.xpath("//option[@id='peach']"));
//elementToBeSelected(Element element)判断"peach"是否被选中
wait.until(ExpectedConditions.elementToBeSelected(select));
//elementToBeClickable(String )判断复选框对象是否可以被点击
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p")));
//presenceOfElementLocated 判断元素是否存在
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//x")));
//textToBePresentInElement判断字样是否在元素中显示
WebElement p = Driver.getCurrentDriver().findElement(By.xpath("//p"));
wait.until(ExpectedConditions.textToBePresentInElement(p, "select ur perfer fruit"));
wait.until(ExpectedConditions.urlContains(""));
wait.until(ExpectedConditions.alertIsPresent());