What is CSS?
CSS stands for Cascading Style Sheets. CSS describes how the web elements present/displayed on web page/screen and look and feel of a web page. Formatting of a webpage.
Why CSS Selector?
CSS refers Cascading style sheet describes the look and feel of a website/web application by maintaining standard formatting for web text, buttons, colouring etc...
So, CSS is uniform to all the pages of a web application and same web application is displayed on different browser. so it uniform to all the web browsers unlike XPATH.
For above css formatters from 1 to 09 we will use facebook.com as reference website / web application where we used to identify/locate control's/web elements by using CSS selectors.
1. By Id
Here For Id CSS uses "#" as a convention symbol.
e.g.
https://www.facebook.com/
" input#email " =>Here we are identifying email Textbox.
" input#pass " => Here, we are identifying password textbox.
" input#u_0_4 " => Here, we are identifying "Log In" button.
Here, we are strictly stating we have to identify only input tag which is having mentioned ID.
or we can use without tag name aswell like below examples-
" #email " => Here we are identifying email Textbox.
" #pass " => Here, we are identifying password textbox.
" #u_0_4 " => Here, we are identifying "Log In" button.
but in this case it will search any control/tag which is having mentioned id.
2. By Class
For class CSS uses " . " (dot) as a convention symbol.
Sometime a control / web elements can have more than one class applied on it (class name seperated by space ). Example- one class specifies height & width another may specifies color and other may specifies formatting. So in such case we have to remove space by adding " ." (dot) in between multiple class names.
e.g.
https://www.facebook.com/
"
Starts-With ( ^= )
In CSS Selector ends-with denoted as ( $= ) like wise XPATH has ends-with() method.
CSS stands for Cascading Style Sheets. CSS describes how the web elements present/displayed on web page/screen and look and feel of a web page. Formatting of a webpage.
Why CSS Selector?
CSS refers Cascading style sheet describes the look and feel of a website/web application by maintaining standard formatting for web text, buttons, colouring etc...
So, CSS is uniform to all the pages of a web application and same web application is displayed on different browser. so it uniform to all the web browsers unlike XPATH.
- Handling of XPATH is different with different browser. XPATH engines which handles xpath is different with different browsers.
- So CSS has advantages over xpath as web application is uniform with different browsers so same CSS locator's will work on different browsers but it is not the same case with xpath means- xpath calculate on IE browsers may/may not work on chrome or firefox browser.
- But Xpath has much more capabilities (like parent, ancestor, self, descendant, preceding-sibling etc..) than css.
- BY Id ( # )
- By Class ( . )
- Sibling (following) ( + )
- Immediate Child ( > )
- Any Child (Child, Grandchild etc..) ( SPACE )
- By Attribute
- Starts-With ( ^= )
- Ends-With ( $= )
- Contains ( *= )
- nth-child(index)
- nth-of-type(index)
For above css formatters from 1 to 09 we will use facebook.com as reference website / web application where we used to identify/locate control's/web elements by using CSS selectors.
1. By Id
Here For Id CSS uses "#" as a convention symbol.
e.g.
https://www.facebook.com/
" input#email " =>Here we are identifying email Textbox.
" input#pass " => Here, we are identifying password textbox.
" input#u_0_4 " => Here, we are identifying "Log In" button.
Here, we are strictly stating we have to identify only input tag which is having mentioned ID.
or we can use without tag name aswell like below examples-
" #email " => Here we are identifying email Textbox.
" #pass " => Here, we are identifying password textbox.
" #u_0_4 " => Here, we are identifying "Log In" button.
but in this case it will search any control/tag which is having mentioned id.
2. By Class
For class CSS uses " . " (dot) as a convention symbol.
Sometime a control / web elements can have more than one class applied on it (class name seperated by space ). Example- one class specifies height & width another may specifies color and other may specifies formatting. So in such case we have to remove space by adding " ." (dot) in between multiple class names.
e.g.
https://www.facebook.com/
"
In CSS Selector starts-with denoted as ( ^= ) like wise XPATH has start-with() method.
It is used to identify any control whose any property/attribute value starts with some fixed character/number. Starting few characters should be fixed ending character can be vary.
It is used to handle dynamic control on a page.
e.g.
- " input[id^='ema'] " =>
It
is used to identify any control whose any property/attribute value ends with some fixed character/number. Ending few characters should
be fixed buts starting or mid characters can be vary.
It is used to handle dynamic control on a page.
e.g.
- "input[name $= 'passwd__']" => Identify New Password from create new account form.
- "input[name $= 'email__']" => Identify Mobile Number or Email address.
- "select[name $= '_day']" => Identify Birthday Day from create new account.
- "button[name $= 'submit'] => Identify Sign Up button from create new account.
9. Contains ( *= )
In CSS Selector contains denoted as ( $= ) like wise XPATH has contains() method.
10. nth-child(index)
nth-child(index) method is used to select/get specified index child element. But the specified index element should be same as mentioned before colon ( : ).
e.g.
Scenario-1:
<ul>
< li > Avinash-1 < /li >
< li > Avinash-2 < /li >
< li > Avinash-1 < /li >
< / ul >
ul>li:nth-child(2) => Here we want to select 2-nd li element which is the child of ul.
Scenario-2:
<ul>
< li > Avinash-1 < /li >
<input type="text" id=i1 />
< li > Avinash-2 < /li >
<input type="text" id=i2 />
< li > Avinash-1 < /li >
< / ul >
ul>li:nth-child(2) => Here we want to select 2-nd li element which is the child of ul. But here child of UL with index=2 is <input> tag so here input tag is not same type of li type both are different tag. So here you will get exception as- (If specified index element type is not same as mentioned before the ( : ) colon then you will get) org.openqa.selenium.NoSuchElementException.
Note:
It means nth-child(index) method check the type of specified index with mentioned in locator befor the ( : ) colon.
11. nth-of-type(index)
nth-of-type(index) is also ued to select the child of specified index. But nth-of-type removes the other tags(child) other than specified tag (child) in selector that is to be select and make list of the specified child tags. Among the list of child elements it will get the specified index element. If the specified index is there then it will return the specified child elements.
If the specified index is not there (out of index ) then it will through exception as- org.openqa.selenium.NoSuchElementException
e.g.
<html>
<body>
<ul>
<li> Avinash-1 </li>
<input type="text" id=i1 />
<li> Avinash-2 </li>
<input type="text" id=i2 />
<li> Avinash-3 </li>
<input type="text" id=i3 />
<input type="text" id=i4 />
</ul>
</body>
</html>
if we want select second li elements then methods looks like as -
<li> Avinash-2 </li>
<li> Avinash-3 </li>
It
is used to identify any control whose any property/attribute value contains some fixed character/number. Starting or Ending few characters should
be vary buts middle few characters can be fixed.
It is used to handle dynamic control on a page.
e.g.
- "input[name *= 'passwd']" => Identify New Password from create new account form.
- "input[name $= '_email_']" => Identify Mobile Number or Email address.
- "select[name $= 'day_']" => Identify Birthday Day from create new account.
- "button[name $= 'sub'] => Identify Sign Up button from create new account.
10. nth-child(index)
nth-child(index) method is used to select/get specified index child element. But the specified index element should be same as mentioned before colon ( : ).
e.g.
Scenario-1:
<ul>
< li > Avinash-1 < /li >
< li > Avinash-2 < /li >
< li > Avinash-1 < /li >
< / ul >
ul>li:nth-child(2) => Here we want to select 2-nd li element which is the child of ul.
Scenario-2:
<ul>
< li > Avinash-1 < /li >
<input type="text" id=i1 />
< li > Avinash-2 < /li >
<input type="text" id=i2 />
< li > Avinash-1 < /li >
< / ul >
ul>li:nth-child(2) => Here we want to select 2-nd li element which is the child of ul. But here child of UL with index=2 is <input> tag so here input tag is not same type of li type both are different tag. So here you will get exception as- (If specified index element type is not same as mentioned before the ( : ) colon then you will get) org.openqa.selenium.NoSuchElementException.
Note:
It means nth-child(index) method check the type of specified index with mentioned in locator befor the ( : ) colon.
11. nth-of-type(index)
nth-of-type(index) is also ued to select the child of specified index. But nth-of-type removes the other tags(child) other than specified tag (child) in selector that is to be select and make list of the specified child tags. Among the list of child elements it will get the specified index element. If the specified index is there then it will return the specified child elements.
If the specified index is not there (out of index ) then it will through exception as- org.openqa.selenium.NoSuchElementException
e.g.
<html>
<body>
<ul>
<li> Avinash-1 </li>
<input type="text" id=i1 />
<li> Avinash-2 </li>
<input type="text" id=i2 />
<li> Avinash-3 </li>
<input type="text" id=i3 />
<input type="text" id=i4 />
</ul>
</body>
</html>
if we want select second li elements then methods looks like as -
- "ul>li:nth-of-type(2)" => It will removes the input tags from above example and make the list of remaining li tags and from that list it will get the element with specified index if it is there . In this case it will return -
- "ul>li:nth-of-type(4)" => Here after removal of input tags (those are other than li tags ) it will make the list as-
<li> Avinash-2 </li>
<li> Avinash-3 </li>
from this list it will check the specified index is there or not. If the specified index is not present then it will return-
org.openqa.selenium.NoSuchElementException
References:
References: