Saturday 3 March 2018

Published March 03, 2018 by

An Overview On cssSelector

An Overview On cssSelector

In this post, we’ll learn what is cssSelector and how to use cssSelector.

What is cssSelector?
CSS stands for Cascading Style Sheets.

HTML Code:- 

                      UN<input type="text"> 
                      PW<input type="password"> 

In the above sample page to identify the password field we can’t use id, name, className, linkText, partialLinkText because they are not present. We can use ‘tagName’ but it has duplicate user field. In this situation we can use cssSelector. CSS stands for Cascading Style Sheets.

cssSelector has following syntax:

Tag[AttributeName = 'AttributeValue'] 
Ex: input[type = 'password']

To check whether CSS expression is correct or not, we can use FirePath’ in Mozilla Firefox.
  1. To install it go to Tools>Add-Ons, search for FirePath.
  2. Click Install button of FirePath and restart the browser.
  3. Open the required web page press F12(Firebug) click on FirePath tab.
  4. Select CSS, type the expression and press Enter.
  5. It will highlight matching elements.
Checking CSS in Google Chrome:
  1. Press F12 in chrome browser, then press Ctr+F.
  2. Type the expression; it will highlight the source code of the matching element. If we place the mouse pointer on the source code, it will highlight the element on the page.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

       public class Demo
                  public static void main(String[] args)
                           WebDriver driver = new FirefoxDriver();

An Overview on Locators:-

An Overview on xPath:-