Selenium WebDriver:使用WebDriver.findElement定位时,等待元素出现 [英] Selenium WebDriver: wait for element to be present when locating with WebDriver.findElement is impossible
问题描述
等待 WebElement
与 WebDriverWait
和 ExpectedConditions
一起显示很方便.
It's convenient to wait for an WebElement
to be present with WebDriverWait
and ExpectedConditions
.
问题是,如果 WebElement.findElment
是定位元素的唯一可能方法,因为它没有ID,没有名称,没有唯一的类?
The problem is, what if WebElement.findElment
was the only possible way to locate the element , 'cause it has no id, no name, no unique class?
WebDriverWait
的构造函数仅接受 WebDriver
作为参数,而不接受 WebElement
.
WebDriverWait
's constructor accepts only WebDriver
as arguments, not WebElement
.
我已经设置了 implicitlyWait
时间,因此使用 try {} catch(NoSuchElementException e){}
似乎不是一个好主意,因为我不知道"不想等那么长时间.
I've set the implicitlyWait
time, so it seems not a good idea to use try{} catch(NoSuchElementException e){}
, 'cause I don't want to wait that long time for this element.
这是场景:
有一个网页,其中的表单包含许多 input
标签.每个 input
标签都有格式要求.
There's one web page with a form containing many input
tags. Each input
tag has a format requirement.
当不满足格式要求时,在此 input
标记之后将出现动态的 div
标记.
A dynamic div
tag would be present after this input
tag when the format requirement is not satisfied.
由于有很多 input
标签,我创建了一个通用方法,如:
As there're so many input
tags, I create a general method like:
public WebElement txtBox(String name) {
return driver.findElement(By.name(name));
}
而不是为每个 input
标签创建数据成员.
instead of creating a data member for each input
tag.
然后我创建一个方法 isValid
来检查某些 input
中的用户输入是否有效.我在 isValid
中要做的就是检查在 inputboxToCheck
之后是否存在一个 div
标记,其代码如下:
Then I create a method isValid
to check whether user inputs in some input
are valid. All I should do in isValid
is to check whether a div
tag is present after inputboxToCheck
, with code like this:
public boolean isValid(WebElement inputboxToCheck) {
WebElementWait wait = new WebElementWait(inputboxToCheck, 1);
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("./following-sibling::div")));
return false;
} catch (TimeOutException e) {
return true;
}
}
WebElementWait
是一个虚构的类(不存在),其作用与 WebDriverWait
相同.
WebElementWait
is an imaginary (not exist) class which works the same way as WebDriverWait
.
推荐答案
上面提到的WebElementWait类:
The WebElementWait class as metioned above:
package org.openqa.selenium.support.ui;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.WebElement;
public class WebElementWait extends FluentWait<WebElement> {
public final static long DEFAULT_SLEEP_TIMEOUT = 500;
public WebElementWait(WebElement element, long timeOutInSeconds) {
this(element, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
}
public WebElementWait(WebElement element, long timeOutInSeconds, long sleepInMillis) {
this(element, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
}
protected WebElementWait(WebElement element, Clock clock, Sleeper sleeper, long timeOutInSeconds,
long sleepTimeOut) {
super(element, clock, sleeper);
withTimeout(timeOutInSeconds, TimeUnit.SECONDS);
pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS);
ignoring(NotFoundException.class);
}
}
与WebDriverWait相同,只是将 WebDriver
参数替换为 WebElement
.
It's the same as WebDriverWait, except that the WebDriver
argument is replaced with WebElement
.
然后是isValid方法:
Then, the isValid method:
//import com.google.common.base.Function;
//import org.openqa.selenium.TimeoutException;
public boolean isValid(WebElement e) {
try {
WebElementWait wait = new WebElementWait(e, 1);
//@SuppressWarnings("unused")
//WebElement icon =
wait.until(new Function<WebElement, WebElement>() {
public WebElement apply(WebElement d) {
return d.findElement(By
.xpath("./following-sibling::div[class='invalid-icon']"));
}
});
return false;
} catch (TimeoutException exception) {
return true;
}
}
这篇关于Selenium WebDriver:使用WebDriver.findElement定位时,等待元素出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!