×

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

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.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

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.

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.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

3 minutes read.

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

5 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-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: 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.

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click...

4 minutes read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

14 minutes read.

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

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes read.