Java Webdriver:ExpectedConditions.invisibilityOf不工作。总是抛出异常
我正在使用Java Webdriver:ExpectedConditions.invisibilityOf不工作。总是抛出异常,java,selenium-webdriver,Java,Selenium Webdriver,我正在使用ExpectedConditions.invisibilityOf来检查其中一个元素的不可见性,但每次它都会抛出超时异常: wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible)); 错误消息: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for invisibility of Proxy element
ExpectedConditions.invisibilityOf
来检查其中一个元素的不可见性,但每次它都会抛出超时异常:
wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible));
错误消息:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for invisibility of Proxy element for: DefaultElementLocator 'By.xpath: //button[text()='button text']' (tried for 30 second(s) with 500 MILLISECONDS interval)
我已经使用element.isDisplayed()
检查了同一元素的可见性,它正确返回'false'
最近我从2.53.0升级了selenium
- Webdriver:3.7.1或3.6.0
- Chrome:62.0.3202.94(官方版本)(64位)
- 操作系统:Windows7
wait.until(ExpectedConditions.invisibilityOfElementLocated(elementTobeInvisible));
或者您可以简单地调用如下方法-
//Wait until Invisibility of element is completed
public void waitForInvisibility(By byElement){
try{
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.invisibilityOfElementLocated(byElement));
//May apply thread sleep for 1 or 2 seconds
}catch(Exception e){}}
在调用此方法之前,按元素声明param,如下所示-
By byElement = By.cssSelector("Use the element's css selector here");
//或
调查了该问题,看起来实施错误:
public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements) {
return new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
Iterator var2 = elements.iterator();
while(var2.hasNext()) {
WebElement element = (WebElement)var2.next();
try {
if (element.isDisplayed()) {
return false;
}
} catch (Exception var5) {
;
}
}
return true;
}
public String toString() {
return "invisibility of all elements " + elements;
}
};
}
对于版本2.53.0和3.7.0,不可见性方法存在实现差异
2.53实施:
public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements) {
return new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
Iterator var2 = elements.iterator();
while(var2.hasNext()) {
WebElement element = (WebElement)var2.next();
try {
if (element.isDisplayed()) {
return false;
}
} catch (Exception var5) {
;
}
}
return true;
}
public String toString() {
return "invisibility of all elements " + elements;
}
};
}
公共静态ExpectedCondition InvisibilityFallElements(最终列表元素){
返回新的ExpectedCondition(){
公共布尔应用(WebDriver WebDriver){
迭代器var2=elements.Iterator();
while(var2.hasNext()){
WebElement=(WebElement)var2.next();
试一试{
if(element.isDisplayed()){
返回false;
}
}捕获(异常变量5){
;
}
}
返回true;
}
公共字符串toString(){
返回“所有元素不可见”+元素;
}
};
}
观察:查看“catch(Exceptionvar5)”行。在这里,我们把所有的东西都当作例外
对于3.7.1实施
public static ExpectedCondition<Boolean> invisibilityOfAllElements(final List<WebElement> elements) {
return new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {
return Boolean.valueOf(elements.stream().allMatch((x$0) -> {
return ExpectedConditions.isInvisible(x$0);
}));
}
public String toString() {
return "invisibility of all elements " + elements;
}
};
}
private static boolean isInvisible(WebElement element) {
try {
return !element.isDisplayed();
} catch (StaleElementReferenceException var2) {
return true;
}
}
公共静态ExpectedCondition InvisibilityFallElements(最终列表元素){
返回新的ExpectedCondition(){
公共布尔应用(WebDriver WebDriver){
返回Boolean.valueOf(elements.stream().allMatch((x$0)->{
返回预期条件。不可见(x$0);
}));
}
公共字符串toString(){
返回“所有元素不可见”+元素;
}
};
}
私有静态布尔值不可见(WebElement元素){
试一试{
return!element.isDisplayed();
}捕获(StaleElementReferenceException var2){
返回true;
}
}
观察:我们只捕获到StaleElementReferenceException,因此在使用该方法时抛出未捕获的TimeoutException,您的问题得到解决了吗?Eles向我们展示代码和相关HTMLyup。看看我的答案。好像是硒虫。目前,我编写了一个自定义方法,将所有内容都捕获为异常及其工作情况。从用户级别使用核心异常不是一个好做法。我们应该只尝试正确使用这个实现。@DebanjanB:是的,你是对的。我已经更改了代码,只捕获所需的异常,而不是核心的“异常”。按钮的类型是什么?如果按钮的类型是button,则可以通过.cssSelector(“button[type='button']”)简单地使用。对于另一种类型的按钮,您只需更改单引号内的类型。