In this post you will learn some CSS rules and how to use CSS selectors and Xpath locators in Selenium.
(1) Id:
An element's id is defined as "[@id='idName']" in Xpath but "#idName" in CSS.
For example, a 'div' element with id 'panel' will be represented as below:
In Xpath locator
(2) Class:
An element's class is defined as "[@class='className']" in Xpath but ".className" in CSS.
For example, a 'div' element with class 'panelClass' will be represented as below:
In Xpath locator
In Xpath locator
(3) Any Attributes:
For selecting an element by its any of the attribute(say "name" is an attribute of a 'div' element and "type" in an "input" element) is given below:
In Xpath locator
(4) Direct Child:
A direct child of an element is denoted as "/" in Xpath and ">" in CSS selector. See the example below for a direct child "li" for a "ul" element:
In Xpath locator
(5) Child or Subchild:
A direct child of an element is denoted as "//" in Xpath and a wehite-space(" ") in CSS selector. See the example below for a child/subchild "li" for a "ul" element:
In Xpath locator
(6) nth Child:
For finding 'nth' child, in Xpath we denote as "[n]" and in CSS we denote as ":nth-of-type(n)". See the below example,
li:nth-child(2)
This means, select an element if It is a paragraph element and the second child of a parent.
li:nth-of-type(2)
This means, select the second paragraph child of a parent. This is less conditional.
See here for more details on difference between the above two.
If we want to select the "fifth li element" (Soft Drinks) in this list, we can use the nth-of-type, which will find the fifth li in the list.
Parent of an element can be represented as "/.." in Xpath and ":parent" in CSS selectors. For example, if you want to indicate parent of the list item of class 'blue'
(8) Next Sibling:
Next sibling is nothing but next adjacent element which is inside the same parent on the page. An adjacent sibling combinator selector allows you to select an element that is directly after another specific element. For example, If you want to select the sibling of "li" element with class "blue" which is 'second' list item,
In Xpath locator
In Xpath locator
(9) Match by Innertext:
There is a javascript methodcontains() which can be used to check the inner text of a web element. For example a link with text "Sign in"
In Xpath locator
(10) Match by Sub-string:
This is interesting and is described below. With this we can match strings with its partial text. E.g. prefix, suffix or any pattern(sub-string)
(1) Id:
An element's id is defined as "[@id='idName']" in Xpath but "#idName" in CSS.
For example, a 'div' element with id 'panel' will be represented as below:
In Xpath locator
//div[@id='panel']In CSS selector
css=div#panel
(2) Class:
An element's class is defined as "[@class='className']" in Xpath but ".className" in CSS.
For example, a 'div' element with class 'panelClass' will be represented as below:
In Xpath locator
//div[@class='panelClass']In CSS selector
css=div.panelClassFor element with multiple class we need to separate with space(" ") in Xpath and dot(".") in case of CSS. See below example,
In Xpath locator
//div[@class='panelClass1 panelClass2']In CSS selector
css=div.panelClass1.panelClass2
(3) Any Attributes:
For selecting an element by its any of the attribute(say "name" is an attribute of a 'div' element and "type" in an "input" element) is given below:
In Xpath locator
//div[@name='continue']
//input[@type='button']In CSS selector
css=div[name='continue']
css=input[type='button']
(4) Direct Child:
A direct child of an element is denoted as "/" in Xpath and ">" in CSS selector. See the example below for a direct child "li" for a "ul" element:
In Xpath locator
//ul/liIn CSS selector
css=ul > li
(5) Child or Subchild:
A direct child of an element is denoted as "//" in Xpath and a wehite-space(" ") in CSS selector. See the example below for a child/subchild "li" for a "ul" element:
In Xpath locator
//ul//liIn CSS selector
css=ul liNote that "ul li" and "ul > li" are different. If you are confusing please go through this article.
(6) nth Child:
For finding 'nth' child, in Xpath we denote as "[n]" and in CSS we denote as ":nth-of-type(n)". See the below example,
In Xpath locator
- Coffee
- Tea
- Milk
- Soup
- Soft Drinks
//ul[@id='drinks']/li[5]In CSS selector
css=ul#drinks li:nth-of-type(5)Also you can use ":nth-child" but the difference between ":nth-child" and ":nth-of-type(n)" is:
li:nth-child(2)
This means, select an element if It is a paragraph element and the second child of a parent.
li:nth-of-type(2)
This means, select the second paragraph child of a parent. This is less conditional.
See here for more details on difference between the above two.
If we want to select the "fifth li element" (Soft Drinks) in this list, we can use the nth-of-type, which will find the fifth li in the list.
css=ul#drinks li:nth-of-type(5)On the other hand, if we want to get the "fifth element" only if it is a li element, we can use a filtered nth-child which will select again (Soft Drinks) here.
css=ul#drinks li:nth-child(5)(7) Parent of an element:
Parent of an element can be represented as "/.." in Xpath and ":parent" in CSS selectors. For example, if you want to indicate parent of the list item of class 'blue'
- first
- second
- third
- fourth
- fifth
//li[@class='blue']/..In CSS selector
css=li.blue:parent
(8) Next Sibling:
Next sibling is nothing but next adjacent element which is inside the same parent on the page. An adjacent sibling combinator selector allows you to select an element that is directly after another specific element. For example, If you want to select the sibling of "li" element with class "blue" which is 'second' list item,
In Xpath locator
//li[@class='blue']/../li[2]In CSS selector
css=li.blue + liSimilarly, if you want to indicate 'third' list item, see the below
In Xpath locator
//li[@class='blue']/../li[3]In CSS selector
css=li.blue + li + li
(9) Match by Innertext:
There is a javascript methodcontains() which can be used to check the inner text of a web element. For example a link with text "Sign in"
In Xpath locator
//a[contains(text(),'Sign in')]or
//a[contains(string(),'Sign in')]In CSS selector
css=a:contains('Sign in')
(10) Match by Sub-string:
This is interesting and is described below. With this we can match strings with its partial text. E.g. prefix, suffix or any pattern(sub-string)
(i) Match a Sub-string(pattern):The summary of sub-string match is listed out in the following table:
Taking an example of a 'div' with an 'id' that contains the text "pattern"
In Xpath locator, we need to use "contains()" to match a sub-string...
//div[contains(@id,'pattern')]In CSS selector, we need to use "*=" for matching a sub-string
css=div[id*='pattern']
(ii) Match a prefix:
Taking an example of a 'div' with an 'id' that starts with the text "prefixString"
In Xpath locator, we need to use "starts-with" to match a prefix...
//div[starts-with(@id,"prefixString")]In CSS selector, we need to use "^=" for matching a prefix
css=div[id^='prefixString']
(iii) Match a suffix:
Taking an example of a 'div' with an 'id' that starts with the text "suffixString"
In Xpath locator, we need to use "ends-with" to match a suffix Note that "ends-with()" is a standard XPath 2.0 function only, it won't work if you are using Xpath 1.0 engine...
//div[ends-with(@id,"suffixString")]In CSS selector, we need to use "$=" for matching a suffix
css=div[id$='suffixString']
Match a Sub-string | Match a Prefix | Match a Suffix | |
---|---|---|---|
Xpath | contains() | starts-with() | ends-with() |
CSS | *= | ^= | $= |
Awesome post :)
ReplyDeleteThank you very much Sir. Very useful post.
ReplyDeleteVery useful information keep update the blog.
ReplyDeletelearn Selenium WebDriver Training in Hyderabad.
Selenium Training in Hyderabad
Thanks for sharing this selenium tricks for css, this tricks are very important, with this tricks we can solve scenario with in the short time. Selenium Online Training
ReplyDeleteYour blog has given me that thing which I never expect to get from all over the websites. Nice post guys!
ReplyDeleteHi to every one, the contents present at this site are
ReplyDeleteactually remarkable for people experience, well, keep up the good work fellows.
School Prospectus Design uk | School Prospectus UK
This comment has been removed by a blog administrator.
ReplyDeleteGreat post, Thanks a lot my friend for very much helpful content to learn CSS and xPATh lcoators.
ReplyDeleteWe been using lots of css in our application, it helped me a lot to understand and some great tricks :)
You rock, thanks a lot.
Best Selenium training in Chennai | Best Selenium training in Chennai
Very informative ..i suggest this blog to my friends..Thank you for sharing
ReplyDeleteSelenium Training in velachery | selenium training and placement in chennai |Selenium Training
Very informative article, thanks a lot for providing a great understanding. It is very helpful especially like me being a beginner. Please keep sharing such articles.
ReplyDeleteSelenium Training in Chennai
Great post! I see the programming coding and step by step execute the outputs.I gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.Java Training institute in Chennai
ReplyDeleteJava Training in Chennai
Java Training with placement in Chennai
Thanks for the detailed description.!
ReplyDeleteGreat Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeletePython Training in Chennai
AngularJS Training in Chennai
Hadoop Training in Chennai
I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyDeleteHadoop Training in Chennai
Big Data Training in Chennai
Python Training in Chennai
Python Training Centers in Chennai
Data Science Training in Chennai
Data Science Course in Chennai
Data Analytics Training in Chennai
Best AngularJS Training in Chennai
AngularJS Training in Chennai
QlikView Training in Chennai
Informatica Training in Chennai
You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information...
ReplyDeleteAngularJS Training Institute in Chennai
AngularJS Cetification Training in Chennai
AWS Training in Chennai
AWS Devops Training in Chennai
DevOps Certification in Chennai
DevOPs Certification Training Institute in Chennai
Best AngularJS Training in Chennai
AngularJS Training in Chennai