×

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class

We will locate the web element with the help of tag and class CSS selector in this section of the tutorials

Where Tag and class are used to find the web element by its tag name using the [.] symbol to locate the class attribute.

The Syntax for Tag and class attribute is as below:

“tag.class”
Or
“.class” 

Where Tag=the html tag of the web element.

[.]=the dot symbol is used when CSS selector contains its Class attribute.

Class = represents the value of its class attribute.

Let us take one example where will perform tag and class attribute of CSS selector:

Steps Actions Input Expected Result
1. Open the Firefox browser.   The Firefox browser should be opened.
2. Navigate to the yahoo login page. www.yahoo.com The yahoo login page must be displayed.
3. Identify the username text box and pass the value. xyz11@yahoo.com The value should be entered.
4. Close the browser.   The browser should be closed.
  • Firstly, we have to Launch Eclipse and open the existing test suite new_test, which we have created in earlier sessions of the WebDriver tutorial.
  • Then right-click on the src folder and create a new Class File from New ? Class.
Selenium web driver-CSS Selector Tag and CLASS
  • And give your Class name as Tag_and_class and click on the Finish button.
Selenium web driver-CSS Selector Tag and CLASS 1

 We are creating our test case step by step to give you a complete understanding of how to use tag and Class Locators to identify a particular web element.

Step1:

  • To access the Firefox browser first, we need to download the gecko driver and set the system property for the gecko driver.
  • We have already discussed this in previous sessions of the tutorial. And you can also refer given link "Working with Firefox browser” for a better understanding that how we download it and set System property for Chrome driver.
// System Property for gecko Driver   
System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
// create an object for FirefoxDriver class.       
WebDriver driver=new FirefoxDriver();   

Step2:

After that, we will write the code to automate our second step, which navigates to the given URL.

The sample code for the navigation of the desired URL:

//navigate to the URL
driver.get(“https://login.yahoo.com”); 

Step3:

 Now, we are trying to locate the desired web element by using the value of its tag and class attribute.

  • Then, right-click on the username text box and then select the Inspect Element.
Selenium web driver-CSS Selector Tag and CLASS 2
  • It will launch a window that contains all the specific codes used in the development of the username text box.
Selenium web driver-CSS Selector Tag and CLASS 3

Where the tag value is input and, the value of its class attribute is phone-no.

Here the sample code:

//identify the username text box and pass the value.
driver.findElement(By.cssSelector("input.phone-no")).sendKeys("admin"); 
Thread.sleep(3000);
System.out.println("username value is entered"); 

Step4:

In the last step of our sample test case is to close the browser.

Here the sample code for closing the browser,

//Close the browser
driver.close();   

Our final test script will look like this:

package testpackage;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Tag_and_class {
public static voclass main(String[] args)throws InterruptedException {
//set the system property
System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-0.25.0-in64\geckodriver.exe");
// create driver object for gecko browser              
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize(); 
//Navigate to yahoo login page              
driver.get("https://login.yahoo.com"");
// identify the username text box and pass the value.
driver.findElement(By.cssSelector("input.phone-no")).sendKeys("xyz11@yahoo.com");
 Thread.sleep(3000);
System.out.println("username value is entered");
//close the browser
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As  ? Java Application.
Selenium web driver-CSS Selector Tag and CLASS 4
  • The above test script will launch the Firefox browser and automate all the test scenarios. 
Selenium web driver-CSS Selector Tag and CLASS 5

 

 


Related Topics

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 minutes read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

4 minutes read.

Selenium WebDriver – Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

2 minutes read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

Selenium WebDriver &ndash; Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.