The following is a comprehensive list of commonly used selectors, ranging from basic to advanced, that I typically employ in my coding practices:
I. Basic
1. DIRECT CHILD
A direct child in XPATH is defined by the use of a “/”, while in CSS, it’s defined using “>”.
Example:
XPATH: //div/a
CSS: div > a
Note:
– A single slash ‘/’ anywhere in XPATH signifies looking for the element immediately inside the parent element.
– A double slash ‘//’ signifies looking for any child or nested child element inside the parent element.
2. CHILD OR SUBCHILD
If an element could be inside another or one of its children, it is defined in XPATH using “//” and in CSS by a whitespace.
For example:
XPATH: //div//a
CSS: div a
3. ID
An element’s id in XPATH is defined using: “[@id=’example’]” and in CSS using: “#”
Eg: xpath = //div[@id=’example’]//a ; css= div#example a
4. CLASS
For class, things are pretty similar in XPATH: “[@class=’example’]” while in CSS it’s just “.”
Eg: xpath= //div[@class=’example’]//a ; css=div.example a
II: Advanced
1. NEXT SIBLING
This 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.
css=form input.username + input
2. ATTRIBUTE VALUES
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.
css=form input[name=’username’]
We can even chain filters to be more specific with our selections using multiple properties (name AND type)
css=input[name=’continue’][type=’button’]
3. CHOOSING A SPECIFIC MATCH
CSS selectors in Selenium allow us to navigate lists.
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.
<p>Heading</p>
Cat
Dog
Car
Goat
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.
css=ul#recordlist li:nth-of-type(4)
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.
css=ul#recordlist li:nth-child(4)
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=ul#recordlist *:nth-child(4)
3.1 Either Element1 or Element2
css=input#username, input#email
4. SUB-STRING MATCHES
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
css=a[id^=’id_prefix_’]
=> A link with an “id” that starts with the text “id_prefix_”
$= Match a suffix
css=a[id$=’_id_sufix’]
=> A link with an “id” that ends with the text “_id_sufix”
*= Match a substring
css=a[id*=’id_pattern’]
=> A link with an “id” that contains the text “id_pattern”
5. XPATH – USING TEXT OF ELEMENT
exact text match: //div[@class=’homepage-hero’]//a[text()=’Enroll now’]
cleaned-up text contains the string: //div[contains(normalize-space(.), ‘Enroll now’)]
6. XPATH – USING CONTAINS
Syntax: //tag[contains(attribute, ‘value’)]
//button[contains(@class, ‘action-btn’)]/span[contains(., ‘Review Instruments’)]
//div[@id=’navbar’]//a[contains(@class,’navbar-link’) and contains(@href,’sign_in’)]
7. XPATH – TRAVERSE UP TO PARENT:
To Its Parrent: xpath-to-some-element//parent::<tag>
TO any Parrent: xpath-to-some-element/ancestor::<tag>
8. XPATH – Preceding Sibling
Syntax: xpath-to-some-element//preceding-sibling::<tag>
9. XPATH – Following Sibling
Syntax: xpath-to-some-element//following-sibling::<tag>
10. CSS Selector Reference
| Selector | Example | Example description |
|---|---|---|
| .class | .intro | Selects all elements with class=”intro” |
| #id | #firstname | Selects the element with id=”firstname” |
| * | * | Selects all elements |
| element | p | Selects all <p> elements |
| element,element | div, p | Selects all <div> elements and all <p> elements |
| element element | div p | Selects all <p> elements inside <div> elements |
| element>element | div > p | Selects all <p> elements where the parent is a <div> element |
| element+element | div + p | Selects all <p> elements that are placed immediately after <div> elements |
| element1~element2 | p ~ ul | Selects every <ul> element that are preceded by a <p> element |
| [attribute] | [target] | Selects all elements with a target attribute |
| [attribute=value] | [target=_blank] | Selects all elements with target=”_blank” |
| [attribute~=value] | [title~=flower] | Selects all elements with a title attribute containing the word “flower” |
| [attribute|=value] | [lang|=en] | Selects all elements with a lang attribute value starting with “en” |
| [attribute^=value] | a[href^=”https”] | Selects every <a> element whose href attribute value begins with “https” |
| [attribute$=value] | a[href$=”.pdf”] | Selects every <a> element whose href attribute value ends with “.pdf” |
| [attribute*=value] | a[href*=”w3schools”] | Selects every <a> element whose href attribute value contains the substring “w3schools” |
| :active | a:active | Selects the active link |
| ::after | p::after | Insert something after the content of each <p> element |
| ::before | p::before | Insert something before the content of each <p> element |
| :checked | input:checked | Selects every checked <input> element |
| :disabled | input:disabled | Selects every disabled <input> element |
| :empty | p:empty | Selects every <p> element that has no children (including text nodes) |
| :enabled | input:enabled | Selects every enabled <input> element |
| :first-child | p:first-child | Selects every <p> element that is the first child of its parent |
| ::first-letter | p::first-letter | Selects the first letter of every <p> element |
| ::first-line | p::first-line | Selects the first line of every <p> element |
| :first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
| :focus | input:focus | Selects the input element which has focus |
| :hover | a:hover | Selects links on mouse over |
| :in-range | input:in-range | Selects input elements with a value within a specified range |
| :invalid | input:invalid | Selects all input elements with an invalid value |
| :lang(language) | p:lang(it) | Selects every <p> element with a lang attribute equal to “it” (Italian) |
| :last-child | p:last-child | Selects every <p> element that is the last child of its parent |
| :last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
| :link | a:link | Selects all unvisited links |
| :not(selector) | :not(p) | Selects every element that is not a <p> element |
| :nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
| :nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
| :nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
| :nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
| :only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
| :only-child | p:only-child | Selects every <p> element that is the only child of its parent |
| :optional | input:optional | Selects input elements with no “required” attribute |
| :out-of-range | input:out-of-range | Selects input elements with a value outside a specified range |
| :read-only | input:read-only | Selects input elements with the “readonly” attribute specified |
| :read-write | input:read-write | Selects input elements with the “readonly” attribute NOT specified |
| :required | input:required | Selects input elements with the “required” attribute specified |
| :root | :root | Selects the document’s root element |
| ::selection | ::selection | Selects the portion of an element that is selected by a user |
| :target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
| :valid | input:valid | Selects all input elements with a valid value |
| :visited | a:visited | Selects all visited links |
Sources:
https://saucelabs.com/resources/articles/selenium-tips-css-selectors