Javatpoint Logo
Javatpoint Logo

CSS Selector - ID/Class and Attribute

  • Syntax: css=<HTML tag><. Or #><value of Class or ID attribute><[attribute=Value of attribute]>
  • '#' is used to symbolize ID attribute. It is mandatory to use hash sign if ID attribute is being used to create CSS Selector.
  • "." is used to symbolize Class attribute. It is mandatory to use dot sign if a class attribute is being used to create CSS Selector.
  • Value of Class or ID attribute - It is the value of ID/Class attribute which is being accessed.
  • We can use value, type and name as an Attribute in the "<[attribute=Value of attribute]>" section.
  • Value of attribute denotes the value which is being accessed at the time of using a particular attribute.
  • We can also add more than two attributes to locate the desired web element. For example, css=input#Passwd[type='password'][name='Passwd'].

Let us consider an example on how to use ID/Class and Attribute to create a CSS Selector which uniquely identifies a web element.

We will try to locate a web element by defining the CSS Selector for that particular web element.

  • Launch Firefox browser.
  • Click on the Selenium icon present on the top right corner on your browser.
  • It will launch the default interface of Selenium IDE.
  • Click on the command text box present on the Test Script Editor Box.

Selenium CSS Selector - ID/Class and Attribute
  • Modify the properties of first command as:

Selenium CSS Selector - ID/Class and Attribute

Now, using second command we will try to locate the "Username" text box.

First we will inspect the "Username" text box to get the value of ID/Class attribute. Subsequently, we will determine the attributes which will be best suited to locate the desired element.

    • Right click on the Username text box and select inspect elements.

Selenium CSS Selector - ID/Class and Attribute
    • It will launch a window containing all the specific codes involved in the development of "Username" text box.

Selenium CSS Selector - ID/Class and Attribute
    • Pick the input tag, value of ID attribute, value of name attribute and value of placeholder attribute.

Selenium CSS Selector - ID/Class and Attribute

We will define the CSS Selector as: css=input#email[name='email_id'][placeholder='Email']

  • Modify the properties of second command as:
    • Command: click at
    • Target: css = input#email[name='email_id'][placeholder='Email']
    • After execution, this command will click on the "Username" text box.

We will add one more command using the same CSS Selector. This command will type the username text into the "Username" text box.

  • Modify the properties of third command as:
    • Command: type
    • Target: css = input#email[name='email_id'][placeholder='Email']
    • Value: username
    • After execution, this command will type the value in the "Username" text box.

Selenium CSS Selector - ID/Class and Attribute

Let us execute our test script to see whether our inserted commands are working or not.

  • Click on the "Run Current Test" button present on the tool bar menu of the IDE. It will execute all of your inserted commands on the browser and gives you an overall summary of the executed test script.

The following screenshot shows that we have successfully executed our test script.


Selenium CSS Selector - ID/Class and Attribute

(1) - Input tag, value of ID attribute, value of name attribute and value of placeholder attribute are used for both of our CSS Selector's to locate the "Username" text box element.

(2) - Log pane displays the overall summary of the executed test script.

(3) - Value generated as a result of execution of our third command.





Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA