过滤ElementsCollection [英] Filtering an ElementsCollection

查看:192
本文介绍了过滤ElementsCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个过滤ElementsCollection的函数,其条件是每个元素的子元素(而不是元素本身)都有条件.

I'm trying to create a function to filter an ElementsCollection, with a condition on a child of each element instead of the element itself.

这是我想出的:

 public static ElementsCollection filterByChild(ElementsCollection elementsCollection, String childCssSelector,
        Condition condition) {

        Predicate<SelenideElement> childHasConditionPredicate = element -> element.$(childCssSelector).has(condition);
        elementsCollection.removeIf(childHasConditionPredicate);
        return elementsCollection;
    }

当这样调用此函数时:

myCollection = SelenideHelpers.filterByChild(myCollection, "a", Condition.text("http://www.link.com");

我收到以下错误消息:

java.lang.UnsupportedOperationException: Cannot remove elements from web page

我在此错误消息上找不到任何可应用于我的代码的相关信息.我想知道为什么会出现此消息.

I did not found any relevant informations on this error message that could be applied to my code. I would like to know why is this message appearing.

推荐答案

检查

要使其正常工作,您需要创建自定义条件,例如:

To make it work you need to create your custom condition like:

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import static com.codeborne.selenide.Selenide.$$;


public static Condition hasChildWithCondition(final By locator, final Condition condition) {
    return new Condition("hasChildWithCondition") {
        public boolean apply(WebElement element) {
            return element.findElements(locator).stream().
                    filter(child -> condition.apply(child)).count() > 0;
        }

        public String toString() {
            return this.name
        }
    };
}

,然后使用它进行过滤:

and then use it to filter:

ElementsCollection collection = $$(".parent");
Condition hasChild = hasChildWithCondition(By.cssSelector("a"), Condition.text("http://www.link.com"));
collection.filterBy(hasChild);

这篇关于过滤ElementsCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆