【问题标题】:ElementNotVisibleException not available in Selenium 4ElementNotVisibleException 在 Selenium 4 中不可用
【发布时间】:2022-08-03 00:35:50
【问题描述】:

我有一些用于等待元素显示的方法,在这些方法的 try/catch 中,catch 是:

throw new ElementNotVisibleException()

我正在从 Selenium 2.53 升级到 Selenium 4,并且 ElementNotVisibleException() 似乎在 Selenium 4 中不可用。我应该用什么替换它? ElementNotInteractableException 是合适的替代品吗?

  • 不是专家,但此异常在 python selenium 4 中可用。所以在 java 中也应该可用
  • 嗯,我想知道为什么它没有解决。我想知道我是否缺少依赖项或其他什么?

标签: java selenium exception


【解决方案1】:

自 4.1.3 版以来,方法 ElementNotVisibleException() 已从硒/java/src/org/openqa/硒

请参阅v4.1.2,其中存在类ElementNotVisibleExceptionElementNotSelectableException

请参阅v4.1.3,其中已删除上述类。

这是由于以下原因:

Selenium 4 对 W3C WebDriver 更加严格,而 W3C WebDriver 在这一点上对可见性无话可说。如果元素不可见,则 W3C 模式下的驱动程序应返回 ElementNotInteractableException

所以使用ElementNotVisibleException.java 类的方法必须更新为使用ElementNotInteractableException.java

例子:

import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public void selectUpload(String uploadType) throws Exception {

    WebElement dropdown = driver.findElement(By.xpath("//select[contains(@name,'uploadFileResultsInput')]"));

    int timeout = 0;
    Exception finalException = null;
    while (timeout < 5000) {
        try {
            new Select(dropdown).selectByVisibleText(uploadType);
            return;
        } catch (ElementNotVisibleException e) {
            finalException = e;
        }
        Thread.sleep(500);
        timeout += 500;
    }
    throw finalException;
}

import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public void selectUpload(String uploadType) throws Exception {

    WebElement dropdown = driver.findElement(By.xpath("//select[contains(@name,'uploadFileResultsInput')]"));

    int timeout = 0;
    Exception finalException = null;
    while (timeout < 5000) {
        try {
            new Select(dropdown).selectByVisibleText(uploadType);
            return;
        } catch (ElementNotInteractableException e) {
            finalException = e;
        }
        Thread.sleep(500);
        timeout += 500;
    }
    throw finalException;
}

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 2019-04-19
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多