Selenide combines 2 ElementsCollections Selenide combines 2 ElementsCollections selenium selenium

Selenide combines 2 ElementsCollections


The API could probably be a little bit more friendly here. But this way you can combine two ElementsCollection instances. The key here is 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);


All add* methods throw UnsupportedOperationException by design. It's because ElementsCollections represents a collection of existing web elements on a web page; and page elements cannot be modified by test. That's why you cannot add or remove elements on the page.

The easiest way is to select all matching elements at once:

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

A little bit longer way is to compose a new list containing both collections:

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

but your goal seems to be doubtful for me. You will get the list with invalid order. Why can it be useful? Why would one need to iterate all elements? I cannot imagine a use case for that.


According to the API:

Note that this implementation throws an UnsupportedOperationException unless add(int, E) is overridden.