In our last Selenium guide, we studied different locator forms. We looked at how to apply: ID, ClassName, Name, Link Text, and XPath locators for pinpointing web elements on a web page.
Building upon that, we will today understand the process of applying CSS Selector as a Locator. This marks our 6th instructional guide in the Selenium Training sequence.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Using CSS Selector as a Locator:
A CSS Selector is an amalgamation of an element selector and a selector value that identifies the web element within a webpage. This combination of an element selector and selector value is referred to as the Selector Pattern.
Selector Pattern is assembled with HTML tags, attributes, and values. The general method to develop CSS Selector and XPath is incredibly similar, the only difference lies in their construction methodology.
Like XPath, CSS selector can also identify web elements without an ID, class, or Name.
Let’s now delve into the main types of CSS Selectors:
What You Will learn:
CSS Selector: ID
In this demonstration, we would target the “Email” text box located in the Gmail.com login form.
The Email textbox bears an ID attribute with a defined value of “Email”. Combined, the HTML tag “input” and the ID attribute value constitute a reference to the “Email Textbox”. This data can thus be used to formulate a CSS Selector.
Creating a CSS Selector for a web element
Step 1: Locate/inspect the desired web element (in this case, the “Email” textbox) and observe that the HTML tag is “input” and the ID attribute has a value of “Email”. These together act as a reference to the “Email Textbox”. This information will hence be utilized to construct a CSS Selector.
Verify the locator value
Step 1: Enter “css=input#Email”, that is, the locator value in the target box in the Selenium IDE and select the Find button. Observe how the Email text box is highlighted.
Syntax
css=<HTML tag><#><Value of ID attribute>
- HTML tag – The tag denoting the web element we aim to reach.
- # – The hash symbol signifies the ID attribute. It is essential to utilize the hash symbol if the ID attribute is being applied to create a CSS Selector.
- Value of ID attribute – This refers to the accessed value of an ID attribute.
- The hash symbol always precedes the ID value.
Note: This syntax is also suitable for other types of CSS Selectors
- When specifying a CSS Selector in the target text box of Selenium IDE, always remember to prefix it with “css=”.
- The order of the above artifacts is unchangeable.
- If two or more web elements share the same HTML tag and attribute value, the first element marked in the page source will be found.
CSS Selector: Class
In this demonstration, we will target the “Stay signed in” checkbox situated beneath the Gmail.com login form.
The “Stay signed in” checkbox has a Class attribute with a defined value of “remember”. Hence, the Class attribute and its value can be employed to formulate a CSS Selector to access the designated web element.
Identifying an element using a Class as a CSS Selector is highly similar to using an ID, with the only difference being in their syntax formulation.
Creating a CSS Selector for the web element
Step 1: Locate/inspect the wanted web element (in this case, the “Stay signed in” checkbox) and observe how the HTML tag is “label” and the Class attribute has a value of “remember”. Combined, these create a reference to the “Stay signed in checkbox”.
Verify the locator value
Step 1: Enter “css=label.remember”, that is, the locator value in the target box in the Selenium IDE and select the Find button. Observe the “Stay signed in” checkbox that is highlighted.
Syntax
css=<HTML tag><.><Value of Class attribute>
- . – The dot symbol represents the Class attribute. It is obligatory to use the dot symbol when creating a CSS Selector with a Class attribute.
- The value of Class always precedes this dot symbol.
CSS Selector: Attribute
In this example, we will reach the “Sign in” button located beneath the Gmail.com login form.
The “Sign in” button has a type attribute with a defined value of “submit”. Hence, the type attribute and its value can be utilized to generate a CSS Selector to pinpoint the designated web element.
Creating a CSS Selector for the web element
Step 1: Locate/inspect the desired web element (in this case, the “Sign in” button) and note that the HTML tag is “input”, the attribute is type, and the value of the type attribute is “submit”. All this together forms a reference to the “Sign in” button.
Verify the locator value
Step 1: Enter “css=input[type=’submit’]”, that is, the locator value in the target box in the Selenium IDE and select the Find button. Observe that the “Sign in” button is now highlighted.
Syntax
css=<HTML tag><[attribute=Value of attribute]>
- Attribute – This is the attribute that we wish to use when constructing a CSS Selector. This could be value, type, name, etc. An attribute should be chosen whose value uniquely distinguishes the web element.
- Value of attribute – This is the accessed value of an attribute.
CSS Selector: ID/Class and attribute
In this example, we will access the “Password” text box situated in the Gmail.com login form.
The “Password” text box has an ID attribute with a value defined as “Passwd” and a type attribute with a value defined as “password”. Consequently, the ID attribute, type attribute, and their values can be used to construct a CSS Selector to reach the designated web element.
Creating a CSS Selector for web element
Step 1: Locate/inspect the necessary web element (in this case, the “Password” text box) and observe that the HTML tag is “input”, the values of the attributes ID and type are “Passwd” and “password” respectively. Together, these create a reference to the “Password” textbox.
Verify the locator value
Step 1: Input “css=input#Passwd[name=’Passwd’]”, that is, the locator value in the target box in the Selenium IDE and hit the Find button. Observe that the “Password” text box has been highlighted.
Syntax
css=<HTML tag><. Or #><value of Class or ID attribute><[attribute=Value of attribute]>
Moreover, two or more attributes can be included in the syntax. An example would be, “css=input#Passwd[type=’password’][name=’Passwd’]”.
CSS Selector: Sub-string
Selenium supports CSS to match parts of a string and thus offers the capability to formulate CSS Selectors using substrings. Three methods can be utilized to create CSS Selectors based on the substring matching technique employed.
Types of techniques
Each of the techniques below has symbolic significance.
- Match a prefix
- Match a suffix
- Match a substring
Let’s go over these in depth.
Match a prefix
This is deployed to match the string using a suitable prefix.
Syntax
css=<HTML tag><[attribute^=prefix of the string]>
- ^ – The symbolic notation for matching a string using a prefix.
- Prefix – This is the string on which the match operation is executed. The expected string is predicted to commence with the given prefix.
As an example: Considering the “Password textbox”, the corresponding CSS Selector would be:
css=input#Passwd[name^=’Pass’]
Match a suffix
This is employed to match the string using a matching suffix.
Syntax
css=<HTML tag><[attribute$=suffix of the string]>
- # – The symbolic notation for matching a string using a suffix.
- Suffix – This is the string upon which the match procedure is executed. The assumed string is predicted to end with the indicated suffix.
As an example, again considering the “Password textbox,” the corresponding CSS Selector would be:
css=input#Passwd[name$=’wd’]
Match a substring
This is utilized to match the string using a matching substring.
Syntax
css=<HTML tag><[attribute*=substring]>
- * – The symbolic notation for matching a string using a substring.
- Substring – This is the string on which the match operation is performed. The expected string is likely to hold the indicated substring pattern.
Again as an example, considering the “Password textbox” once more, the corresponding CSS Selector would be:
css=input#Passwd[name$=’wd’]
CSS Selector: Inner text
The inner text permits us to identify and create a CSS Selector using a string pattern that the HTML Tag exhibits on the webpage.
The “Need help?” hyperlink located beneath the Gmail.com’s login form serves as an example.
The HTML tag representing the hyperlink contains encapsulated text. Therefore, this text can be used to generate a CSS Selector to access the desired web element.
Syntax:
css=<HTML tag><:><contains><(text)>
<
ul>