×

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute

We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial.

Where Tag and attribute are used to find the web element by its Tag name and attribute.

The Syntax for Tag and Attribute is as below:

Tagname[attribute=’value’]

Where Tag name=the html tag of the web element.

[ ] = the square brackets are used to place the value of a specific attribute.

Attribute= id, class, and name, their values can be used as an attribute.

Value=value of the selected attribute.

Let us take one example where we will perform actions on the tag and attribute of CSS selector locator:

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Facebook login page. https://www.facebook.com The Facebook login page must be displayed.
3. Identify the username text box and pass the value. Username=xyz11@gmail.com The username text box should be identified, and the value should be entered.
4. Identify the password text box and pass the value. Password=1234#45 The Password text box should be identified, and the value should be entered.
5. 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 session of the WebDriver tutorial.
  • Then, right-click on the new_test folder and create a new Class File from New ? Class.
Selenium web driver-CSS Selector Tag and Attribute
  • And give your Class name as Tag_and_attribute and click on the Finish button.
Selenium web driver-CSS Selector Tag and Attribute 1

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

Step1:

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

Step2:

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

The sample code for the navigation of the desired URL:

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

Step3:

 For locating the username text box of the Facebook login page, we will use the value of its tag and name attribute as:

  • Right-click on the username text box and clicks on the Inspect Element.
Selenium web driver-CSS Selector Tag and Attribute 2
  • It will launch a developer tool window that contains all the specific codes, which are used in the development of the username text box.
Selenium web driver-CSS Selector Tag and Attribute 3

Where the tag name value is input and, the value of its name attribute is email.                                                                                   

Here the sample code:

//identify the username text box and pass the value.
driver.findElement(By.cssSelector("input[name=email]")).sendKeys("xyz11@gmail.com");
Thread.sleep(3000);
System.out.println("username value is entered"); 

Step4:

For locating the password text box of the Facebook login page, we will use the value of its tag and name attribute as:               

  • First, right-click on the password text box, and click on the Inspect Element field.
Selenium web driver-CSS Selector Tag and Attribute 4
  • It will launch a developer tool window that contains all the specific codes which are used in the development of the password text box.
Selenium web driver-CSS Selector Tag and Attribute 5

Where the tag value is input, and the value of its name attribute is pass.

Here the sample code:

//identify the password text box and pass the value.    
driver.findElement(By.cssSelector("input[name=pass]")).sendKeys("1234@45");
Thread.sleep(3000);
System.out.println("password value is entered"); 

Step5:

 In the last step of our sample test case, we are closing the existing browser.

//Close the browser
driver.close();   

Our final test script will look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Tag_and_attribute {
public static void main(String[] args)throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");                         
// create an object for Chrome Driver    
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//navigate to the URL
driver.get("https://www.facebook.com");
//Identify the username textbox and passing the value
driver.findElement(By.cssSelector("input[name=email]")).sendKeys("xyz11@gmail.com");
Thread.sleep(3000);
System.out.println("username value is entered");
 //identify the password text box and passing the value
driver.findElement(By.cssSelector("input[name=pass]")).sendKeys("1234@45");
Thread.sleep(3000);
System.out.println("password 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.
  • And we can see from the below screenshot, that the above test script will launch the Google Chrome browser, and automate all the test scenarios.
Selenium web driver-CSS Selector Tag and Attribute 6

Related Topics

WebDriver-Id Locator

WebDriver-Id Locator: The Id Locator is used to identify any web element in UI with the help of the ID attribute. The First preference is always goes to ID locator while inspecting...

4 minutes read.

Selenium WebDriver - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh. Whenever a WebDriver launches a browser, all...

5 minutes read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

Selenium WebDriver-CSS Selector: Tag and ID

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

4 minutes read.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute 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 - 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.

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-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium WebDriver – 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.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

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 Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 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.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

Selenium WebDriver- Radio button handling

Selenium WebDriver- Radio button handling In this tutorial, we will be learning about handling the radio button in selenium WebDriver. Let us take one example to give you a better understanding of...

3 minutes read.