The core of the library. Main methods are
$(String cssSelector)– returns object of the SelenideElement class that represents first element found by CSS selector on the page.
$(By)– returns "first SelenideElement" by the locator of the By class.
$$(String cssSelector)– returns object of type ElementsCollection that represents collection of all elements found by a CSS selector
$$(By)– returns "collection of elements" by the locator of By type.
Usually, when you get a SelenideElement object by the Dollar
$ command, you can perform some action on it:
or even several actions at once:
or you can check some condition:
"Double Dollar" command (
$$) can be useful when a needed element is a one of a same type. For example, instead of:
you can use more readable and verbose alternative:
Such "composite locator" has the following advantage. In case of error during locating an element, it will result in more informative message. Since we broke down the locator into smaller parts, the error will show exactly which part caused the fail. And in case of "one long solid locator" we would be able to know that "all locator" did not work, with no any idea about which part really caused the fail.
In addition, if we need to work with different elements of the same collection, we now can "put" a collection to a variable:
ElementsCollection resultLinks = $$("#search-results a"); //... resultLinks.first().shouldHave(text("selenide.org")); resultLinks.get(1).shouldHave(text("ru.selenide.org")); resultLinks.shouldHave(size(10)); //... resultLinks.findBy(text("github.com/codeborne/selenide")).click()
Now we do not duplicate the
"#search-results a" locator in different places. Therefore, if this part changes - we will perform corresponding changes only in one place.
Moreover, now having a separate name for the locator you can use the "autocomplete" feature of your IDE to type
Don't be shy to search for more methods inside the Selenide class that can suit your needs. Just type in your IDE
Selenide. and choose the needed option among available IDE proposals.
Here are just a few examples: