Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
CSS matchers give Selenium WebDriver the ability to search the DOM using complex criteria. In this video we will cover how to construct complex queries using CSS and to discuss what we can do with CSS that could not be done with previous finders
Learn more
Concerns
If you are not sure if the browser you are testing supports your pseudo-class you can check on Can I Use. For instance, if you wanted to check the is the :valid
pseudo class, you'd search for it. The Mozilla Developer Network also provides Browser Compatibility tables.
-
0:00
CSS selectors are super powerful for making concise, specific, and
-
0:04
understandable DOM queries.
-
0:06
The more you get familiar with them, the easier your automation will become.
-
0:10
You'll find you can query just about anything.
-
0:13
We have quite a few courses here just on CSS alone, so
-
0:16
make sure to check those in the teacher's notes.
-
0:18
Ready?
-
0:18
Let's get practicing.
-
0:21
Okay, so I still have my browser open and
-
0:23
I thought that we navigate around a bit using the Finder in Chrome dev tools.
-
0:28
So again, that's here in few, it's a Cmd+F or
-
0:31
Ctrl+F in the element tab and we'll show this row.
-
0:36
Things here. All right, so
-
0:39
what do you say that we get a whole of this check box like we just did but
-
0:43
we do it with CSS as opposed to the chained finds that we have done before?
-
0:47
So let's take a look on how we could get in there.
-
0:49
So first, it's inside of this div, right?
-
0:52
It's inside of this div class main and then, it's inside of this div, and
-
0:57
then it's this checkbox.
-
0:59
Let's start working from the top.
-
1:03
So, the way that you specify a class in CSS is with a dot.
-
1:07
So, let's do .main.
-
1:09
There we go. So, we got that div.
-
1:12
Okay? And then we can just do input.
-
1:15
Awesome, so that works, but look at this, it's matching three, right?
-
1:20
If I press this down arrow it will show that I'm catching those that
-
1:24
are in the other checkboxes.
-
1:25
So, we don't want those.
-
1:27
And just specifying that it's kind of dangerous, right?
-
1:30
Cuz we want just specifically that checkbox.
-
1:33
If we did find element, it would just get that, but let's be more specific.
-
1:37
So, it's inside of a div, so it's div main class, it's inside of a div.
-
1:40
So, let's go ahead and try to do that let's say div.
-
1:44
And that says one of one.
-
1:46
So, let's read that.
-
1:48
So what it says is for all elements with the class of main find all descendant
-
1:56
that are of div, and then of that find all descendants that are of input.
-
2:01
Now you might have noticed that I said descendants.
-
2:03
In CSS selectors they like to think of HTML tree structure as a family.
-
2:09
So the parent of the input box, this input box here, is this div, right?
-
2:14
It's parent.
-
2:15
And this div has two children, label and input, right?
-
2:19
So it's like a family tree.
-
2:21
So, I guess this would be the grandparent but these are all descendants, right?
-
2:26
Even if this was the grandparent, these are the descendants.
-
2:28
All of these fields here are the descendants.
-
2:31
So maybe we want to get a little bit more specific about our tree.
-
2:35
So we really only want this div here.
-
2:38
If any other div was added anywhere in the descendants of this main class,
-
2:42
we'd end up selecting it.
-
2:44
And we really only want a direct child at will,
-
2:46
like a direct child of this class main.
-
2:49
Well, as you might have guess by now, we can specify that.
-
2:52
So, if between the main and the div here, between this,
-
2:58
if we add a greater than sign, what that points out,
-
3:02
let's get rid of this input so we can show that it's selecting.
-
3:05
What that points out is that this is a direct child,
-
3:08
this says from the class main giving a direct child of div.
-
3:12
And then within that div let's search for input.
-
3:16
And we can see over here that it's only returning one.
-
3:18
So, I'm gonna grab our selector here and I'll put it in the code.
-
3:24
We'll put it as a locator and we'll call that
-
3:29
toggleNoneRespondersVisiblity: By.css,
-
3:35
I'll paste in our finder there.
-
3:38
And now that we've got that locator, let's use it.
-
3:41
So, let's write a function so
-
3:43
that we can abstract away the knowledge of the page, just like we did for addInvitee.
-
3:47
So we'll say function toggleNonResponderVisibility,
-
3:52
we used the same word there, so that's a function, right?
-
3:55
And we will go ahead and find the element.
-
3:59
FindElement and we'll do locators.toggleNonResponderVisibility and
-
4:05
we will click that.
-
4:11
Perfect.
-
4:13
All right, so then let's go ahead and recall that.
-
4:16
We'll say toggleNonResponderVisibility, and we'll call that function.
-
4:21
Let's open this up.
-
4:21
I'm gonna go ahead and close out.
-
4:23
Looks like I have two of these running, I'm gonna quit both of these.
-
4:28
Those are kept separate from my other Google Chrome that I have opened
-
4:31
elsewhere.
-
4:32
Okay, so, let's go ahead and I'll use the up arrow, and I will kick that off.
-
4:36
Let's move over to that, and here we go.
-
4:39
And so, you'll notice that it's checked just like we wanted.
-
4:41
And there it is they're there, they're just checking in there.
-
4:45
Nice.
-
4:46
So let's challenge ourselves a bit here.
-
4:48
Let's see if we can get a handle on this remove button, right?
-
4:52
So first, let's just see what the button CSS does, okay?
-
4:56
So let's take a look, we'll inspect this.
-
5:00
And I'll close this.
-
5:02
Let's bring this down a little bit here.
-
5:05
I'm going to search in here.
-
5:07
I'm going to make this a little bit bigger for everybody.
-
5:10
All right, so we're looking for this button and
-
5:12
specifically this remove button.
-
5:14
Okay, so first things that we need to do is let's just try from the top, so
-
5:17
we'll say it's a button.
-
5:19
Okay, there's five buttons.
-
5:21
Okay, it's also grabbing the form registrar at the top.
-
5:24
We don't want that, so we definitely want it inside of this invited list, so
-
5:28
let's build that.
-
5:30
So we'll say invitedList as the ID, and now we say button.
-
5:37
Now there's only four,
-
5:38
and it's the edit and the remove of each of these, so that's good.
-
5:44
So this says all descendants of the invited list that are buttons,
-
5:49
but there's four now, so it has edit and remove.
-
5:52
So there's a couple of ways that we can find this, so let's explore a few.
-
5:56
So, what we want is the element that follows the edit button.
-
5:59
Now this is called a sibling.
-
6:02
That makes sense, right?
-
6:03
This list item here is the parent, and these are the children and
-
6:08
these are siblings.
-
6:10
They look like they get along better than my kids.
-
6:12
Okay, so
-
6:13
if you want to get the next sibling after the one that you have selected.
-
6:16
So let's go ahead and remember this is starting at here.
-
6:19
So we'll say get the next sibling.
-
6:23
There we go and this is awesome, right?
-
6:25
Cuz there's two, there's only two.
-
6:26
So both of those it's the next sibling after the first button.
-
6:30
So we have the remove button which is awesome.
-
6:32
So, another trick that we could do instead of using the sibling there,
-
6:35
is we can use what is known as a pseudo class.
-
6:38
Pseudo classes are keywords that you can add a selector to specify a state.
-
6:43
Now in our case, we want the last button in each of these LIs.
-
6:49
So what we can do is we can say, invitedList button.
-
6:53
So that's finding the first button, each of these and we're gonna say last-child.
-
6:58
And that does the same thing as the following sibling was doing.
-
7:01
So we get a hold of our remove buttons.
-
7:05
So, CSS provides a wonderful concise way to explain what you were trying to select.
-
7:10
Now, I've put lots of great information in the teacher's notes of this video.
-
7:13
I want to make sure that you know that selenium is using your
-
7:16
browser's CSS engine.
-
7:18
So, if you are driving an older browser,
-
7:20
be cautious about what selectors you are using.
-
7:24
See the teacher's notes for more on that concern.
-
7:27
And one more thing that I wanted to add.
-
7:29
If you want to test your selector's programatically like in the console,
-
7:32
you can do this.
-
7:33
So let's go to the console and I can use $$ and that will let me do a CSS selector.
-
7:40
So #invitedList, button: last-child using that pseudo selector there.
-
7:47
If I do that it will return those two buttons, right?
-
7:50
And nice thing about Chrome is if I highlight over it shows me what I got.
-
7:55
Just a different way of doing it if you don't want to use that search thing.
-
7:57
So now let's say that I wanted to find a specific list item by the specific
-
8:02
content, right?
-
8:03
Like let's say that I wanted to grab Steve Hunter here and
-
8:06
I wanted to click remove, right?
-
8:07
Do you think we could do that?
-
8:10
Well we can, and it's time to revisit Xpath, right after this quick break.
You need to sign up for Treehouse in order to download course files.
Sign up