×

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 the help of tag name like input tag, anchor tag, button tag, etc. on the web pages.

The syntax of the tag name locator is as below:

driver.findElement(By.tagName(“value of tag name”));

Let us see one sample test case in which we will try to identify the tag element using tag name locator to automate the following scenarios:

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Irctc application home page. https://www.irctc.co.in/ The irctc home page must be displayed.
3. Print the size and name of the text value.   The size and the name of the text value should be displayed.
4. Close the Browser.   The Browser should be closed.
  • After launching the Eclipse, we will open the existing test suite new_test, which we have created in earlier session of the WebDriver tutorial.
  • And then, right-click on the src folder and create a new Class File from New ? Class.
right-click on the src folder and create a new Class File 3
  • And now, we will save the class name as Tag_name and click on the Finish button.
we will save the class name as Tag_name

Now we will create our test case step by step to have an overview of how to use tag name locators to identify a web element.

Step1:

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

Step2:

After that, we will move to our second step, which navigates us to the given URL.

Here the sample code,

//navigate to the URL
driver.get("https://www.irctc.co.in/nget/train-search "); 

Step3:

Now we are trying to locate web link count and name of the link using anchor tag by using the tag name locator.

//locating the number of links using anchor tag
List lst= driver.findElements(By.tagName("a"));
//print the link count
System.out.println(lst.size());
//display all the link names
 for(int i=0; i


Step 4:

  • In the last step of our sample test case, we will terminate the existing browser,

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;
public class Tag_name { 
public static void main(String[] args) throws InterruptedException {
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver(); 
driver.manage().window().maximize(); 
//Navigate to the home page irctc application
driver.get("https://www.irctc.co.in");
//locating the number of links 
 List lst= driver.findElements(By.tagName("a"));
//print the link count
System.out.println(lst.size()); 
//display all the link names
for(int i=0; i


  • To run the test script in Eclipse, right-click on the code and then select Run as ? Java Application.
  • After automating all the test scenarios, we can see the output of print commands in the Eclipse console window as shown in the below snapshot.
we can see the output of print commands in the Eclipse console window

 

 


Related Topics

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

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

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.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes 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-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-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

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

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.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

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 WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

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

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.

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

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.

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.