Basic Concepts of Selectors
Last updated
Last updated
Let's take the search button from the Wikipedia website as an example.
For the record, for a website like Wikipedia it's fully sufficient to simply copy selectors, as described here: Copy Selectors. Building custom selectors is only needed if you create TaskBots for "tricky" websites such as social media. We are just using Wikipedia as an example to explain the basic concepts and the way of building your own CSS selectors.
Open your console by right-clicking on the search icon as shown below and then click on Inspect.
Now let's zoom in on the highlighted element on the right:
Every CSS selector has a tag. Examples can be div, a, main, li, etc. It's the first word that is marked purple (see picture above).
For Wikipedia's search button, the tag is button.
Most CSS selectors have several attributes such as class, id, aria-label, etc. One selector can have one, many or no attributes. Attributes are marked brown (see picture above).
For Wikipedia's search button, the attributes are class and type.
Inside the attributes there is a value.
For Wikipedia's search button, the value inside of class is pure-button pure-button-primary-progressive and the value inside of type is submit.
To build a selector, you need to adhere to this structure:
tag[attribute='value']
Note that there is no space between tag and attribute in square brackets. Note that the value is wrapped in quotes.
Or, if your element has several attributes, then the structure can be:
tag[attribute1='value'][attribute2='value']
So for Wikipedia's search button in the above example, the selector would look like this:
button[class='pure-button pure-button-primary-progressive'][type='submit']