Skip to content
CodeKruPro

Locate a heading by its tag name

Automation taskEasyAsked in: Accenture, Capgemini, TCS, Wipro

A tag name such as h2 or tr is rarely unique, so interviewers ask when it is useful. The common answer: to read every row, link or header cell inside a parent element you have already found. Here you find the page heading (an h2) by its tag name and print its visible text.

Find Element by Tag Name

Video walkthrough

Watch on YouTube

Find Element by Tag Name

The By.tagName() locator finds elements by their HTML tag. It's useful for finding headings, paragraphs, or specific form elements.

On the Text Fields page, the main heading is an <h2> tag.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the h2 element by tag name
  3. Print its visible text

Expected output:

Heading: Text Fields