Selenium Tips - CSS Selectors in Selenium Demystified
Following my previous Selenium testing Tip of The Week about improving your locators, this blog post will show you some advanced CSS rules and pseudo-classes that will help you move your XPATH locators to CSS, a native approach on all browsers.
Our first example is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that's inside the same parent. Let's show an example using a form to select the field after username.
Let's write a css selector that will choose the input field after "username". This will select the "alias" input, or will select a different element if the form is reordered.
css=form input.username + input
If you don't care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value. A good example would be choosing the 'username' element of the form without adding a class.
We can easily select the username element without adding a class or an id to the element.
We can even chain filters to be more specific with our selections.
Here Selenium will act on the input field with name="continue" and type="button"
Choosing a specific match
CSS selectors in Selenium allow us to navigate lists with more finess that the above methods. If we have a ul and we want to select its fourth li element without regard to any other elements, we should use nth-child or nth-of-type.
If we want to select the fourth li element (Goat) in this list, we can use the nth-of-type, which will find the fourth li in the list.
On the other hand, if we want to get the fourth element only if it is a li element, we can use a filtered nth-child which will select (Car) in this case.
Note, if you don't specify a child type for nth-child it will allow you to select the fourth child without regard to type. This may be useful in testing css layout in selenium.
CSS in Selenium has an interesting feature of allowing partial string matches using ^=, $=, or *=. I'll define them, then show an example of each:
|^=||Match a prefix|
|$=||Match a suffix|
|*=||Match a substring|
A link with an "id" that starts with the text "id_prefix_"
A link with an "id" that ends with the text "_id_sufix"
A link with an "id" that contains the text "id_pattern"
Matching by inner text
And last, one of the more useful pseudo-classes, :contains() will match elements with the desired text block:
This will find the log out button on your page no matter where it's located. This is by far my favorite CSS selector and I find it greatly simplifies a lot of my test code. Tune in next week for more Selenium testing tips from Sauce Labs. Sauce Labs is the leading cloud testing platform that allows you to test across 700+ browsers/OS platforms in the cloud. With Selenium IDE and Selenium RC compatibilities, you can get complete cross-platform browser testing today.
- Accessibility Testing
- Appium Resources
- Best Practices
- Continuous Delivery
- Continuous Integration
- Continuous Testing
- Cross Browser Testing
- Guest Blog Posts
- Load Testing
- Mobile Development & Testing
- News & Product Updates
- Open Sauce
- Open Source
- Performance Testing
- Product Updates
- Quality Assurance
- Quality Engineering
- Sauce Product Info
- Security Testing
- Selenium Resources
- Software Development & Testing
- The Story of Sauce