Selenium:检查是否存在元素

时间:2018-05-30 17:35:31

标签: selenium

在实时自动化中,在对它们执行某些操作之前,我们是否检查每个元素的存在(在测试中)?

只要有findElement语句,就有可能 NoSuchElementException 。我的问题是我们是否每次检查元素是否存在?

每个findElement语句是否需要被try-catch块包围?

5 个答案:

答案 0 :(得分:1)

有两种情况需要考虑:

  1. 元素是否存在;这意味着它存在于DOM中。
  2. 元素是否可见;意思是它在DOM中,没有隐藏或等效标志。
  3. 对于第一种情况,我使用以下辅助方法:

    this.waitForElement = function(locator) {
        browser.wait(function() {
          return browser.isElementPresent(locator);
        }, testData.Timeout.TWO_MINUTES);
    };
    

    这将等待与提供的定位器匹配的元素出现的任意时间(它存在于DOM中)。

    对于第二种情况,我使用这种辅助方法:

    this.waitForElementIsVisible = function(el){
        let EC = protractor.ExpectedConditions;
        browser.wait(EC.visibilityOf(el), testData.Timeout.TWO_MINUTES, "Element did not become visible after 2 minutes");
    };
    

    这需要WebElement作为单个参数,并等待元素变为可见(它存在于DOM中,不会通过css样式或其他东西隐藏)

    作为奖励,我还发现这个辅助方法对于以下形式测试错误状态非常有用:

    this.waitForElementIsClickable = function(el){
        let EC = protractor.ExpectedConditions;
        browser.wait(EC.elementToBeClickable(el), testData.Timeout.TWO_MINUTES, "Element did not become clickable after 2 minutes");
    };
    

    WebElement作为第一个参数,并等待直到可以单击该WebElement。

    注意,我使用的是量角器,并在这些片段中引用了Protractor。因此,除非您使用量角器,否则通过直接复制+粘贴可能无法100%运行。应该很容易调整它们以适应您的设置。

答案 1 :(得分:1)

您可能会发现使用课程AbstractWebDriverEventListener特别有用。此类实现接口WebDriverEventListener,该接口为WebDriver触发的事件定义beforeafter挂钩。

可以实现钩子beforeFindBy之前的一个来检查元素的存在。例如:

public void beforeFindBy(By by, WebElement element, WebDriver driver) {
    // Explicit wait to check for the presence of the element using the "by" locator
}

类似地,可以实现before hook beforeClickOn以在对该元素执行click事件之前检查该元素是否可被点击。

答案 2 :(得分:1)

让我试着逐一回答你的问题:

  • In real-time automation, do we check for the presence of every element(in test) before performing some action on them根据最佳做法,每当用户被重定向到新页面时,您需要根据所需操作确保元素的状态

有3个最广泛使用的ExpectedConditions可以与WebDriverWait结合使用,以验证元素的状态如下:

presenceOfElementLocated

presenceOfElementLocated(By locator)定义如下:

public static ExpectedCondition<WebElement> presenceOfElementLocated(By locator)

Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

visibilityOfElementLocated

visibilityOfElementLocated(By locator)定义如下:

public static ExpectedCondition<WebElement> visibilityOfElementLocated(By locator)

Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

elementToBeClickable

elementToBeClickable(By locator)定义如下:

public static ExpectedCondition<WebElement> elementToBeClickable(By locator)

Description : An expectation for checking an element is visible and enabled such that you can click it.
  • Wherever there is a findElement statement, there is a chance of NoSuchElementException,完全没有。如果你构建了一个合适的Locator Strategy,那么你就不会面对NoSuchElementException

    您可以在此处找到有关NoSuchElementExeption, selenium unable to locate element

  • 的详细讨论
  • Does every findElement statement need to be surrounded by try-catch block,并非总是如此。如果 usecase 涉及处理肯定否定方案,则try-catch {}阻止是完美的。

答案 3 :(得分:0)

检查Web元素是否存在的最直接,最简单的方法是使用findElements()(注意复数)返回一个数组,即

if (driver.findElements(By.xpath("//a")).isEmpty())
    // no links exist

可以使用相同的方法检查单个元素,它不必是数组。返回的.size()为零意味着没有匹配,因此所需的元素不在DOM中。

有些人会争辩说,一个更好的方法是将.findElement包装在一个方法中,该方法会执行try / catch并相应地记录/响应各种结果,例如找不到元素或陈旧元素等。

我在我的测试中使用这两种方法,并使用我自己的.findElements逻辑存在函数,但在其他场景中使用包装器。

答案 4 :(得分:0)

简短答案:否。 更长的答案:一种方法是等到“页面完成”指示符使用明确的等待。这将表明您可以安全找到所需的Element。如果这些元素之一不可用,则测试将失败,表明找不到元素。 在测试中根据需要使用Explicit等待(当您期望页面/ DOM更改时)。

但是,对此问题还是有一些看法的答案。在整个过程的每个步骤中检查/等待元素都存在,这确实花费了运行时间,并且可能会延长测试的时间,而该测试可能会更快地失败。

相关问题