【问题标题】:Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working抛出 NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) 的 Selenium invisibilityOf(element) 方法不起作用
【发布时间】:2018-08-16 17:05:30
【问题描述】:

此查询包含 2 个相关问题。 在进行下一步之前,我需要等待一个元素不可见,因此我尝试定义一个自定义方法,如下所示:

public void waitToDisappear(long timeOutInSeconds, WebElement element) {
    WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
    wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
    wait.until(ExpectedConditions.invisibilityOf(element));
}

当我将此方法称为common.waitToDisappear(5, <WebElement>); 时,我得到的是Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:。 但是,如果我使用定位器方法new WebDriverWait(world.driver, 5).until(ExpectedConditions.invisibilityOfElementLocated((By.xpath(someXpath)))),它可以正常工作,没有任何异常。

问题 1:NoSuchElementExceptioninvisibilityOfElementLocated() 的 Selenium 实现中被忽略,但在 invisibilityOf() 中却没有。这有什么原因吗?但是,我认为这就是我得到例外的原因。如何等待元素(不是定位器)消失?

问题 2:为什么我得到 NoSuchElementException,即使我使用的是 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);。这是使用wait.ignoring的正确方法吗?看来wait.ignoring() 在这里什么都没做。

提前感谢您的回答。

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

    invisibilityOf()

    invisibilityOf(WebElement element) 定义为:

    public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)
    
    An expectation for checking the element to be invisible
    

    这里的期望是,元素必须 present 以及 visible 作为前置条件,并且该方法将等待元素出现 不可见。此时值得一提的是,由于参数是 WebElement 类型,findElement(By by) 必须成功定位元素作为前提条件。因此NoSuchElementException 不能忽略


    invisibilityOfElementLocated()

    invisibilityOfElementLocated(By locator) 定义为:

    public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator)
    
    An expectation for checking that an element is either invisible or not present on the DOM.
    

    这里的期望显然是元素在HTML DOM 中已经不可见不存在。在这种情况下,主要任务是元素的缺席,它甚至可能在 ExpectedCondition 被调用之前或在 timespan 期间发生,而 ExpectedCondition 处于活动状态。所以这里我们需要忽略NoSuchElementException作为强制措施。


    回答问题2:使用wait.ignoring(org.openqa.selenium.NoSuchElementException.class); 是不合理的,因为调用invisibilityOf(WebElement element) 的先决条件涉及元素必须存在于DOM Tree 中作为强制性的事实测量。

    【讨论】:

    • 感谢您的快速回复。我正在尝试等待“加载”(一旦我进入页面,该元素在显示所有数据之前可见)元素消失,然后再单击另一个元素。我可以看到 visibilityOf(element) 返回 true(这意味着找到元素),但 invisibilityOf(element) 正在抛出 NoSuchElementException
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多