
✅Mastering CSS Selectors:
“Optimize Web Element Identification with Advanced CSS Selectors in Selenium”.
Selenium WebDriver Java Example:-
✅Complete Guide to CSS Selectors in Selenium:
“Boost Test Automation Efficiency Using Master-Level CSS Selectors in Selenium”
✅What is a CSS Selector in Selenium?
CSS Selectors in Selenium are powerful string patterns used to identify web elements based on their HTML structure. Unlike other locators like ID or Name, CSS Selectors can locate elements even when these attributes are missing, making them a preferred choice for advanced Selenium users.
CSS Selectors provide flexibility and precision by allowing combinations of HTML tags, class names, IDs, attributes, and even inner text. Below, we explore the most commonly used CSS Selector formats in Selenium.
✅Types of CSS Selectors in Selenium with Examples:
“Enhance Locator Strategies: Mastering CSS Selectors for Selenium Testing”.
✅1. Tag and ID – CSS Selector:
This method uses the HTML tag along with the element’s unique ID. The ID is always prefixed with a #
.
Syntax:
csscss=tag#id
tag
= HTML tag of the element#
= Hash symbol, always used before the IDid
= The unique ID of the element
Example:
- Suppose we want to locate the email input field on Facebook’s login page:
csscss=input#email
Steps:
- Navigate to Facebook.
- Use browser developer tools to inspect the “Email or Phone” input box.
- Notice its tag is
input
and ID isemail
. - Use
css=input#email
in Selenium, and it should highlight the element.
✅2. Tag and Class – CSS Selector:
When an element has a class name, we use a dot (.
) instead of a hash (#
).
Syntax:
csscss=tag.class
tag
= HTML tag of the element.
= Dot symbol, always used before the class nameclass
= The class name of the element
Example:
If the input field has a class name inputtext
:
csscss=input.inputtext
Steps:
- Navigate to Guru99 Facebook Test Page.
- Inspect the “Email or Phone” input field.
- Note that its tag is
input
and class isinputtext
. - Use
css=input.inputtext
in Selenium to locate the element.
⚠ Note: If multiple elements share the same tag and class, Selenium identifies the first one in the HTML source.
✅3. Tag and Attribute – CSS Selector:
This method identifies an element based on a specific attribute and its value.
Syntax:
csscss=tag[attribute=value]
tag
= HTML tag of the element[ ]
= Square brackets enclosing the attribute-value pairattribute
= Any valid HTML attribute (e.g., name, placeholder, type)value
= The value of the chosen attribute
Example:
Locating the “Last Name” input field on the Mercury Tours registration page:
csscss=input[name=lastName]
Steps:
- Go to Guru99 Mercury Tours Registration Page.
- Inspect the “Last Name” input field.
- Identify its tag (
input
) and attribute (name="lastName"
). - Use
css=input[name=lastName]
in Selenium to locate it.
📌 Tip: If multiple elements share the same tag and attribute, only the first occurrence is recognized.
✅4. Tag, Class, and Attribute – CSS Selector:
Combining tag, class, and attribute allows for more precise element selection.
Syntax:
csscss=tag.class[attribute=value]
tag
= HTML tag of the element.
= Dot before the class nameclass
= The class of the element[ ]
= Square brackets enclosing the attribute-value pairattribute=value
= The chosen attribute and its corresponding value
Example:
Identifying the “Email or Phone” input field using class and tabindex:
csscss=input.inputtext[tabindex=1]
Steps:
- Navigate to Guru99 Facebook Test Page.
- Inspect the input field’s class (
inputtext
) and attribute (tabindex="1"
). - Use
css=input.inputtext[tabindex=1]
in Selenium. - Modify
tabindex=2
to locate the Password field: cssCopyEditcss=input.inputtext[tabindex=2]
✅5. Inner Text – CSS Selector:
This method selects elements based on their visible text content.
Syntax:
csscss=tag:contains("inner text")
tag
= HTML tag of the elementinner text
= The text displayed inside the element
Example:
Locating the “Password” label on Mercury Tours’ homepage:
csscss=font:contains("Password:")
Steps:
- Visit Guru99 Mercury Tours Homepage.
- Inspect the “Password” label.
- Notice it has no ID, name, or class.
- Use
css=font:contains("Password:")
in Selenium.
This method also works with partial text:
csscss=font:contains("Boston")
If multiple elements share the same inner text, only the first one is identified.
✅Mastering CSS Selectors Summary Table: CSS Selectors in Selenium:
Method | Syntax | Example |
---|---|---|
Tag and ID | css=tag#id | css=input#email |
Tag and Class | css=tag.class | css=input.inputtext |
Tag and Attribute | css=tag[attribute=value] | css=input[name=lastName] |
Tag, Class, and Attribute | css=tag.class[attribute=value] | css=input.inputtext[tabindex=1] |
Inner Text | css=tag:contains("text") | css=font:contains("Password:") |
✅Mastering CSS Selectors Conclusion:
“Improve Selenium Test Accuracy with Powerful CSS Selector Techniques”.
CSS Selectors in Selenium provide a powerful and flexible way to locate web elements, especially when IDs and names are unavailable. By mastering different CSS Selector strategies, you can make your Selenium scripts more robust and efficient.