Selenide结合了2个ElementsCollections

时间:2015-12-17 13:42:34

标签: selenium selenide

我有2 ElementsCollectionsoddTableRowItemsevenTableRowItems

private static ElementsCollection oddTableRowItems() {
    return $$(By.className("odd"));
}

private static ElementsCollection evenTableRowItems() {
    return $$(By.className("even"));
}

我想将2组合起来,只进行一次for循环。它是行项目,只有类名称因样式而异,我只能通过类名来识别它们。

这就是我尝试组合它的方式 - 但它不起作用:

ElementsCollection rowElements = evenTableRowItems();
rowElements.addAll(oddTableRowItems());

我得到了:

  

java.lang.UnsupportedOperationException

是否有人如何合并2 ElementsCollections

4 个答案:

答案 0 :(得分:2)

这里的API可能会更友好一些。但是这样你就可以组合两个ElementsCollection实例。这里的关键是WebElementsCollectionWrapper class。

ElementsCollection evenElements = $$(By.className("even"));
ElementsCollection oddElements = $$(By.className("odd"));
List<SelenideElement> elementsCombined = new ArrayList<>(evenElement);
elementsCombined.addAll(oddElements);
WebElementsCollectionWrapper wrapper = new WebElementsCollectionWrapper(elementsCombined);
ElementsCollection selenideCollectionCombined = new ElementsCollection(wrapper);

答案 1 :(得分:2)

所有add*方法都按设计抛出UnsupportedOperationException。这是因为ElementsCollections代表网页上现有网络元素的集合;和页面元素不能通过测试修改。这就是为什么你不能在页面上添加或删除元素的原因。

最简单的方法是一次选择所有匹配的元素:

$$(".odd,.even").shouldHave(size(10));

稍微长一点的方法是撰写一个包含两个集合的新列表:

List<String> newList = new ArrayList<String>();
newList.addAll($$(".odd"));
newList.addAll($$(".even"));

但你的目标似乎对我来说是值得怀疑的。您将获得包含无效订单的列表。它为什么有用?为什么需要迭代所有元素?我无法想象一个用例。

答案 2 :(得分:0)

根据API

  

请注意,除非重写add(int,E),否则此实现会抛出UnsupportedOperationException。

答案 3 :(得分:0)

您可以尝试使用此代码。很好!

ArrayList<SelenideElement> newList = new ArrayList<SelenideElement>();
newList.addAll(Selenide.$$(By.className("odd"));
newList.addAll(Selenide.$$(By.className("even"));