Professional Documents
Culture Documents
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
Search
How to Locate Elements in Chrome and IE Browsers for Building Selenium Scripts Selenium
Tutorial #7
Selector Pattern is constructed using HTML tags, attributes and their values. The central theme behind
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
1/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
the procedure to create CSS Selector and Xpath are very much similar underlying the only difference
in their construction protocol.
Like Xpath, CSS selector can also locate web elements having no ID, class or Name.
So now gearing ahead, let us discuss the primitive types of CSS Selectors:
CSS Selector: ID
In this sample, we would access Email text box present in the login form at Gmail.com.
The Email textbox has an ID attribute whose value is defined as Email. Thus ID attribute and its
value can be used to create CSS Selector to access the email textbox.
Creating CSS Selector for web element
Step 1: Locate / inspect the web element (Email textbox in our case) and notice that the html tag is
input and value of ID attribute is Email and both of them collectively make a reference to the
Email Text box. Hence the above data would be used to create CSS Selector.
Syntax
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
2/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
Syntax
css=<HTML tag><.><Value of Class attribute>
. - The dot sign is used to symbolize Class attribute. It is mandatory to use dot sign if Class
attribute is being used to create CSS Selector.
The value of Class is always preceded by a dot sign.
3/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
In this sample, we would access Sign in button present below the login form at gmail.com.
The Sign in button has a type attribute whose value is defined as submit. Thus type attribute and
its value can be used to create CSS Selector to access the designated web element.
Creating CSS Selector for web element
Step 1: Locate / inspect the web element (Sign in button in our case) and notice that the html tag is
input, attribute is type and value of type attribute is submit and all of them together make a
reference to the Sign in button.
Verify the locator value
Step 1: Type css=input[type=submit] i.e. the locator value in the target box in the Selenium IDE
and click on the Find Button. Notice that the Sign in button would be highlighted.
Syntax
css=<HTML tag><[attribute=Value of attribute]>
Attribute It is the attribute we want to use to create CSS Selector. It can value, type, name
etc. It is recommended to choose an attribute whose value uniquely identifies the web element.
Value of attribute - It is the value of an attribute which is being accessed.
The Password text box has an ID attribute whose value is defined as Passwd, type attribute whose
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
4/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
value is defined as password. Thus ID attribute, type attribute and their values can be used to create
CSS Selector to access the designated web element.
Creating CSS Selector for web element
Step 1: Locate / inspect the web element (Password text box in our case) and notice that the html
tag is input, attributes are ID and type and their corresponding values are Passwd and password
and all of them together make a reference to the Password textbox.
Verify the locator value
Step 1: Type css=input#Passwd[name=Passwd] i.e. the locator value in the target box in the
Selenium IDE and click on the Find Button. Notice that the Password text box would be
highlighted.
Syntax
css=<HTML tag><. Or #><value of Class or ID attribute><[attribute=Value of attribute]>
Two or more attributes can also be furnished in the syntax. For example,
css=input#Passwd[type=password][name=Passwd].
5/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
6/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
This is one of the most frequently used strategies to locate web element because of its simplified
syntax.
Owing to the fact that creating CSS Selector and Xpath requires a lot of efforts and practice, thus the
process is only exercised by more sophisticated and trained users.
Next Tutorial #7: Proceeding ahead with our next tutorial, we would take the opportunity to
introduce you with an extension of locating strategies. Thus, in the next tutorial, we would study the
mechanism to locate web elements on Google Chrome and Internet Explorer.
We are covering Selenium Locators in more details as it is important part of Selenium Script creation.
Let us know your queries/comments below.
15
Like
13
Share
11
Tweet
Selenium
Selenium Video Tutorials
Software Testing Class
Recommended reading:
How to Locate Elements in Chrome and IE Browsers for Building Selenium Scripts
Selenium Tutorial #7
How to Identify Web Elements Using Selenium Xpath and Other Locators Selenium
Tutorial #5
Handling Web Tables, Frames, and Dynamic Elements in Selenium Script Selenium
Tutorial #18
How to Use Firebug for Creating Selenium Scripts Selenium Tutorial #4
Usage of Selenium Select Class for Handling Dropdown Elements on a Web Page Selenium
Tutorial #13
Check Visibility of Web Elements Using Various Types WebDriver Commands Selenium
Tutorial #14
Debugging Selenium Scripts with Logs (Log4j Tutorial) Selenium Tutorial #26
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
7/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
9 comments
#1 Zoltan
Good one. it will be nice to provide tips on moving xpath locators to css as well.
#2 Shilpa Kamble
when should we use CSS selector method? any guidelines for using locators?
#3 Shruti Shrivastava
@Zoltan
Thank you for your kind words.
You can find the information related to Xpaths in the previous tutorial.
#4 Shruti Shrivastava
@Shilpa
Thats a nice question.
There is obviously a preference order in which we tempt to use these elements
IDs are considered to be the most reliable locator type as they are unique but they doesnt
work well if the ids are dynamic.
Links are also reliable but can only be used with anchor tags.
Xpaths are reliable and can easily locate elements but they tend to be slower than other
locator types.
CSS Selectors are faster than Xpaths but are complex to create.
Thus, people widely uses Xpaths and cssSelector because of their flexibility..
#5 Subrata Paul
Hi Shruti,
Great job!!! I am enjoying it a lot. Would you go into the complex concepts of Selenium so that
we can become proficient in working with selenium at workplaces?
Syntax before CSS Selector: Inner text needs to be corrected. It is the same as Match the
suffix.
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
8/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
Leave a Comment
Name
Mail
Website (Optional)
Submit
Get FREE
eBooks +
Blog
Updates
Enter Email:
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
9/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
Recommended eBook
Learn & Master Software Testing Quickly from Experts Click here to know more.
Adv
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
10/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
Latest Articles!
How to Figure Out What QA Job Best Suits You Based on Your Skills?
11 Ways You Know Youre a Tester..
Case Study: How to Eliminate Flaws of Waterfall and Agile Development Processes
Using a Hybrid Model
8 Proven Ways to Manage Stress as a Software Testing Professional
What is Software Testing All About? (Read This 10 Point Summary Carefully)
How to Perform White Box Testing Explained with a Simple Example
How to Plan and Manage Testing Projects Effectively (Tips)
CSS Example
CSS3 Tutorial
Parent Child Class
START HERE!
Home
Get FREE Updates
Testing RESOURCES
QA Testing TRAINING
Premium eBook
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
11/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
FREE eBooks
ALL Articles
Contact Us
What Readers Say About Us
Your Feedback!
QA Forum
Categories
Select Category
Follow Us!
Follow @VijayShinde
Follow
Like
6.3k
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
12/13
3/17/2015
How to Use CSS Selector for Identifying Web Elements for Selenium Scripts Selenium Tutorial #6 Software Testing Help
Search
http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
13/13