Class ElementsCollection

java.lang.Object
com.codeborne.selenide.ElementsCollection

@ParametersAreNonnullByDefault public class ElementsCollection extends Object
  • Constructor Details

    • ElementsCollection

      public ElementsCollection(CollectionSource collection)
    • ElementsCollection

      public ElementsCollection(Driver driver, Collection<? extends org.openqa.selenium.WebElement> elements)
    • ElementsCollection

      public ElementsCollection(Driver driver, String cssSelector)
    • ElementsCollection

      public ElementsCollection(Driver driver, org.openqa.selenium.By seleniumSelector)
  • Method Details

    • should

      @Nonnull public ElementsCollection should(CollectionCondition... conditions)
      Check if a collection matches given condition(s).

      For example:

       
       $$(".text_list").should(containExactTextsCaseSensitive("text1", "text2"));
       $$(".cat_list").should(allMatch("value==cat", el -> el.getAttribute("value").equals("cat")));
       
       
    • should

      @Nonnull public ElementsCollection should(CollectionCondition condition, Duration timeout)
      Check if a collection matches a given condition within the given time period.
      Parameters:
      timeout - maximum waiting time
    • shouldBe

      @Nonnull public ElementsCollection shouldBe(CollectionCondition... conditions)
      For example: $$(".error").shouldBe(empty)
    • shouldBe

      @Nonnull public ElementsCollection shouldBe(CollectionCondition condition, Duration timeout)
    • shouldHave

      @Nonnull public ElementsCollection shouldHave(CollectionCondition... conditions)
      For example: $$(".error").shouldHave(size(3)) $$(".error").shouldHave(texts("Error1", "Error2"))
    • shouldHave

      @Nonnull public ElementsCollection shouldHave(CollectionCondition condition, Duration timeout)
      Check if a collection matches given condition within given period
      Parameters:
      timeout - maximum waiting time
    • should

      @Nonnull protected ElementsCollection should(String prefix, Duration timeout, CollectionCondition... conditions)
    • waitUntil

      protected void waitUntil(CollectionCondition condition, Duration timeout)
    • filter

      @CheckReturnValue @Nonnull public ElementsCollection filter(WebElementCondition condition)
      Filters collection elements based on the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • filterBy

      @CheckReturnValue @Nonnull public ElementsCollection filterBy(WebElementCondition condition)
      Filters collection elements based on the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • exclude

      @CheckReturnValue @Nonnull public ElementsCollection exclude(WebElementCondition condition)
      Filters elements excluding those which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • excludeWith

      @CheckReturnValue @Nonnull public ElementsCollection excludeWith(WebElementCondition condition)
      Filters elements excluding those which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • find

      @CheckReturnValue @Nonnull public SelenideElement find(WebElementCondition condition)
      Find the first element which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      SelenideElement
      See Also:
    • findBy

      @CheckReturnValue @Nonnull public SelenideElement findBy(WebElementCondition condition)
      Find the first element which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      SelenideElement
      See Also:
    • texts

      @CheckReturnValue @Nonnull public List<String> texts()
      Gets all the texts in elements collection
      See Also:
      • NOT RECOMMENDED Instead of just getting texts, we highly recommend to verify them with $$.shouldHave(texts(...));.
    • attributes

      @CheckReturnValue @Nonnull public List<String> attributes(String attribute)
      Gets all the specific attribute values in elements collection
      See Also:
      • NOT RECOMMENDED Instead of just getting attributes, we highly recommend to verify them with $$.shouldHave(attributes(...));.
    • get

      @CheckReturnValue @Nonnull public SelenideElement get(int index)
      Gets the n-th element of collection (lazy evaluation)
      Parameters:
      index - 0..N
      Returns:
      the n-th element of collection
      See Also:
    • first

      @CheckReturnValue @Nonnull public SelenideElement first()

      returns the first element of the collection (lazy evaluation)

      NOTICE: Instead of $$(css).first(), prefer $(css) as it's faster and returns the same result

      Returns:
      the first element of the collection
      See Also:
    • last

      @CheckReturnValue @Nonnull public SelenideElement last()
      returns the last element of the collection (lazy evaluation)
      Returns:
      the last element of the collection
      See Also:
    • first

      @CheckReturnValue @Nonnull public ElementsCollection first(int elements)
      returns the first n elements of the collection (lazy evaluation)
      Parameters:
      elements - number of elements 1…N
      See Also:
    • last

      @CheckReturnValue @Nonnull public ElementsCollection last(int elements)
      returns the last n elements of the collection (lazy evaluation)
      Parameters:
      elements - number of elements 1…N
      See Also:
    • size

      @CheckReturnValue public int size()

      return actual size of the collection, doesn't wait on collection to be loaded.

      Returns:
      actual size of the collection
      See Also:
    • snapshot

      @CheckReturnValue @Nonnull public ElementsCollection snapshot()
      Takes the snapshot of current state of this collection. Succeeding calls to this object WILL NOT RELOAD collection element from browser.

      Use it to speed up your tests - but only if you know that collection will not be changed during the test.

      Returns:
      current state of this collection
      See Also:
    • asFixedIterable

      @CheckReturnValue @Nonnull public ElementsCollection.SelenideElementIterable asFixedIterable()
      Returns a "static" Iterable which doesn't reload web elements during iteration. It's faster than asDynamicIterable()}, but can sometimes can cause StaleElementReferenceException etc. if elements are re-rendered during the iteration.
      Since:
      6.2.0
      See Also:
    • asDynamicIterable

      @CheckReturnValue @Nonnull public ElementsCollection.SelenideElementIterable asDynamicIterable()
      Returns a "dynamic" Iterable which reloads web elements during iteration. It's slower than asFixedIterable(), but helps to avoid StaleElementReferenceException etc.
      Since:
      6.2.0
      See Also:
    • as

      @CheckReturnValue @Nonnull public ElementsCollection as(String alias)
      Give this collection a human-readable name

      Caution: you probably don't need this method. It's always a good idea to have the actual selector instead of "nice" description (which might be misleading or even lying).

      Parameters:
      alias - a human-readable name of this collection (null or empty string not allowed)
      Returns:
      this collection
      Since:
      5.20.0
    • toString

      @CheckReturnValue public String toString()
      Overrides:
      toString in class Object