×

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

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.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

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

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

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

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

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

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

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.

Selenium WebDriver- Checkbox handling

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

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

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