检查SelenideElementIterator的设计方式:
@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove elements from web page");
}
要使其正常工作,您需要创建自定义条件,例如:
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
}
};
}
然后使用它进行过滤:
ElementsCollection collection = $$(".parent");
Condition hasChild = hasChildWithCondition(By.cssSelector("a"), Condition.text("http://www.link.com"));
collection.filterBy(hasChild);