Hey guys! In this post, we will learn how to work with web elements using Selenide. We will look into different locator strategies such as find by ID, CSS Selector as well as XPath.
The $ command
All Selenide elements journey begins with the
$ command. To work with any selector you will add
$(selector) and then take any action on that selector such as
This is equivalent to doing
driver.findElement(selector) for those of you that are familiar with Selenium Java.
Check out the video below to learn about how to work with elements in Selenide:
Find by ID
To find an element by ID in Selenide, you can do it using the
The above command will command will find an ID called “get-started” in the DOM and then perform a
click action on it.
Find by CSS Selector
By default with Selenide, any string you add in the selector, it will consider that as a CSS Selector. For example –
.shouldHave(text("Think different. Make different."));
With CSS Selector, you do not need to provide
By.something which makes working with css selectors quite easy.
Find by XPath
XPath works similarly to ID, you need to specify that you are trying to find element by xpath like this –
So these are the three most common strategies you will be using to find element in Selenide. Simply add in the
$ command and provide the selector strategy after that and Selenide will take care of finding that element and returning that back to you.
Thanks for reading!