【问题标题】:Check element existence with WebDriver findElements().isEmpty method使用 WebDriver findElements().isEmpty 方法检查元素是否存在
【发布时间】:2015-09-09 08:39:50
【问题描述】:

我需要检查页面上是否存在某些元素。
我已经看到了这个WebDriver: check if an element exists? 问题,但我想知道为什么不简单地应用findElements().isEmpty 方法?
我认为它会做同样的工作。

UPD 现在我看到findElements().isEmpty 工作得很好,所以我只是想知道为什么要寻找其他更复杂的方法,而有一个简单的方法呢?

【问题讨论】:

  • 接受的答案建议driver.findElements( By.id("...") ).size() != 0!driver.findElements( By.id("...") ).isEmpty() 完全相同。你的问题是什么?
  • 如果是这样,为什么没有人提到isEmpty 方法(至少我没有看到),只使用size 方法呢?此外,我尝试应用 isEmpty 方法,但到目前为止我发现它并没有像我想象的那样工作。
  • 他们在答案下方的 cmets 中提到了它。所以,是的,使用isEmpty() 而不是size() == 0 通常是更好的做法,静态分析工具甚至会将其作为警告指出。你说得对,isEmpty() 是更好的选择。不过,在引擎盖下,它做同样的事情。

标签: java selenium selenium-webdriver


【解决方案1】:

findElements 返回匹配给定选择器的所有元素的列表。所以你在这里使用isEmpty()方法内置的java列表。

【讨论】:

  • 检查可见性!= 检查是否存在。
  • 是的,没错。我没有说它是,或者它没有被问到
  • 原始问题没有说明任何关于检查可见性的内容。它询问是否存在。只是澄清。对于您的示例,如果它不存在,则会引发异常。
  • 不,你的权利它没有。但它不要求检查元素是否存在。 Eliyahu 询问 isEmpty() 是什么,我在第一行回答了。
  • 我问“我想检查页面上是否存在某些元素。”只有在那之后我才问“那么 isEmpty 是做什么用的以及如何使用的?” :)
【解决方案2】:

isEmpty() 实际上来自 Java 的 List 类,因为 findElements() 返回 WebElements 的 List

【讨论】:

  • ...这就是为什么应该首选driver.findElements(selector).isEmpty()。它更短,并且避免使用异常进行流控制,这总体上是一个很好的做法。
【解决方案3】:
 public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds) {
    WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
    wait.until(ExpectedConditions.presenceOfElementLocated(selector));
    return findElement(driver, selector);
  }


  public static WebElement findElementSafe(WebDriver driver, By selector, long timeOutInSeconds) {
    try {
      return findElement(driver, selector, timeOutInSeconds);
    } catch (TimeoutException e) {
      return null;
    }
  }

【讨论】:

  • 使用 Google Guava 和/或 Java 8,您的 findElementSafe() 方法可能会更好地使用 Optional 类作为返回值,而不是潜在的 null
  • 无论如何,虽然您为“如何检查元素是否存在”添加了一个相当不错的答案,但您没有回答 OP 的问题 “那么 isEmpty 用于什么以及如何使用?”
【解决方案4】:

以下代码在 WebDriver 的情况下可以正常工作

if(driver.findElements(By.xpath(xpath)).size()!=0)
{
    ...
}

但是如果我们使用 WebElementFacade,我们可以写一些类似下面的东西,这要容易得多。

@FindBy(xpath = "xpathvalue")
private WebElementFacade element;

if(element.isPresent())
{
    ...
}

【讨论】:

  • 为什么你认为这比我在主题中提到的最简单的 findElements().isEmpty 更有效?
  • 如果我们使用 WebElementFacade,我们不必获取调用 findElements() 方法所需的驱动程序。
  • 好吧,但我什至不知道 WebElementFacade 是什么。我稍后会读到它。谢谢!
【解决方案5】:

通过限制页面中元素的搜索范围,我找到了最好的方法,例如这个函数搜索任何标签名称,你想要获取这些标签的文本并将其存储在 ArrayList 中,然后你必须要做的是将要证明页面中不存在的元素文本与 ArrayList 值匹配。 - 如果在数组中找不到元素文本,这意味着您的测试通过:元素不存在/显示在页面中。

/**
 * @params : tagName, elementText
 * @desc: To check the Elements is not Exist in the page - by actual tagName for element and the expected element text
 */
public void verifyElementIsNotExistByTagNameAndElementText(String tagName, String elementText) {
    ArrayList<String> arrList;
    List<WebElement> listOfTags = driver.findElements(By.tagName(tagName));
    arrList = listOfTags.stream().map(WebElement::getText).collect(Collectors.toCollection(ArrayList::new));
    System.out.println(ConsoleColors.BLACK_BOLD+"All TagNames Text: "+arrList+ConsoleColors.RESET);
    if (!arrList.contains(elementText)) {
        System.out.println(ConsoleColors.GREEN_BOLD+"PASS: "+ConsoleColors.BLACK_BOLD+elementText+ConsoleColors.BLACK+" is not exist in the page"+ConsoleColors.RESET);
    } else {
        System.out.println(ConsoleColors.RED_BOLD+"FAIL: "+ConsoleColors.BLACK_BOLD+elementText+ConsoleColors.BLACK+" is exist in the page"+ConsoleColors.RESET);
    }
}

【讨论】:

  • 我看不出这如何回答我多年前提出的问题?顺便说一句,现在我可以更好地回答和描述这个问题以及我那个时期的许多其他问题......
猜你喜欢
  • 2011-09-25
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 2012-12-05
  • 1970-01-01
  • 2012-11-20
相关资源
最近更新 更多