简体   繁体   English

从 List 中断言该元素<webelement>包含一些文字

[英]Assert that element from List<webelement> contains some text

I'm not sure how can I assert that I have a list of webelements where each webelement has the text inside , And I want to verify that on this list of webelements exists some text.我不确定如何断言我有一个 webelements 列表,其中每个 webelement inside都有文本,我想验证这个 webelements 列表中是否存在一些文本。

I not sure how this should looks assertion in this case.我不确定在这种情况下这应该如何断言。

Simple get stream, map to string (text) and check if any matches:简单的获取流,映射到字符串(文本)并检查是否有匹配项:

boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .anyMatch(text -> "inside".equals(text));

To be safe filter out null's and trim:为了安全过滤掉空值和修剪:

boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .filter(Objects::nonNull)
  .map(String::trim)
  .anyMatch(text -> "inside".equals(text));

You can do it like this:你可以这样做:

public boolean isElementsContainsProvidedText(String text){

    List<WebElement> elements = driver.findElements(By.xpath("your_xpath_for_list"));

    return elements.stream().anyMatch(e->e.gettext().trim().equals(text));     

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium web 驱动程序断言网络元素包含文本并显示实际与预期 - Selenium web driver assert webelement contains text and display actual vs expected 从列表中获取文本 <WebElement> 在Groovy - Get text from List<WebElement> in Groovy 如何断言某个字符串至少包含列表中的一个值<string> ?</string> - How to assert that some String contains at least one value from the List <String>? 无法将Webelement List中的一个元素与Array list元素匹配 - Could not match one element from Webelement List with Array list element Junit:断言一个列表包含至少一个与某些条件匹配的属性 - Junit: assert that a list contains at least one property that matches some condition 如何在List中查找WebElement <WebElement> 通过文字? - How to find WebElement in a List<WebElement> by text? Junit - 断言列表包含 - Junit - assert the list contains 断言列表仅包含一个满足条件的元素 - Assert list contains exactly one element satisfying condition Selenium WebDriver:List中每个WebElement中的findElement() <WebElement> 总是返回第一个元素的内容 - Selenium WebDriver: findElement() in each WebElement from List<WebElement> always returns contents of first element 在Excel中仅打印Web元素列表中的一个元素 - Printing only one element from webelement list in excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM