use*_*405 11 java selenium selenium-webdriver
它的方便,以等待WebElement在场与WebDriverWait和ExpectedConditions.
问题是,如果找到元素的唯一可行方法是什么WebElement.findElment,因为它没有id,没有名字,没有唯一的类?
WebDriverWait的构造函数只接受WebDriver参数而不是WebElement.
我已经设定了implicitlyWait时间,因此使用它似乎不是一个好主意try{} catch(NoSuchElementException e){},因为我不想等待这个元素的那么长时间.
这是场景:
有一个网页,其中包含许多input标签的表单.每个input标签都有格式要求.
当不满足格式要求时,div此input标记之后将出现动态标记.
由于input标签太多,我创建了一个通用方法,如:
public WebElement txtBox(String name) {
return driver.findElement(By.name(name));
}
Run Code Online (Sandbox Code Playgroud)
而不是为每个input标签创建数据成员.
然后我创建一个方法isValid来检查某些用户输入是否input有效.我应该做的isValid就是检查div标签后是否存在inputboxToCheck,代码如下:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
WebElementWait是一个虚构的(不存在的)类,其工作方式与WebDriverWait.
use*_*405 10
上面提到的WebElementWait类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
它与WebDriverWait相同,只是WebDriver参数被替换为WebElement.
然后,isValid方法:
//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;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否对你有所帮助,但它允许等待元素你想要多少时间.
public WebElement findDynamicElement(By by, int timeOut) {
WebDriverWait wait = new WebDriverWait(driver, timeOut);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
return element;
}
findDynamicElement(By.xpath("//body") , 30);
Run Code Online (Sandbox Code Playgroud)
小智 5
user2432405 解决方案的一个更通用的变体是使用 SearchContext 类型而不是 WebElement:
public class SearchContextWait extends FluentWait<SearchContext> {
...
Run Code Online (Sandbox Code Playgroud)
这允许在 WebDriver 和 WebElement 上进行等待,类似于 SearchContext 接口是 WebDriver 和 WebElement 的祖先。isValid 方法也需要调整:
...
WebElement icon = wait
.until(new Function<SearchContext, WebElement>() {
public WebElement apply(SearchContext d) {
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,您失去了 ExpectedConditions.xxxx() 方法的所有便利,因为它们在内部使用 WebDriver 接口。
| 归档时间: |
|
| 查看次数: |
58318 次 |
| 最近记录: |