Bird
Raised Fist0
Selenium Javatesting~20 mins

findElement by tagName in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
TagName Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of findElement by tagName usage
What will be the output of the following Selenium Java code snippet when executed on a webpage with multiple
Selenium Java
WebElement button = driver.findElement(By.tagName("button"));
System.out.println(button.getText());
ASubmit
BNoSuchElementException
CAll button texts concatenated
Dnull
Attempts:
2 left
💡 Hint
findElement returns the first matching element found in the DOM.
❓ assertion
intermediate
2:00remaining
Correct assertion for findElement by tagName
Which assertion correctly verifies that the first

element on the page contains the text 'Welcome'?

Selenium Java
WebElement header = driver.findElement(By.tagName("h1"));
AassertFalse(header.getText().isEmpty());
BassertTrue(header.getText().contains("Welcome"));
CassertEquals("Welcome", header.getText());
DassertNotNull(header.getText());
Attempts:
2 left
💡 Hint
Use exact text match for verification.
❓ locator
advanced
2:00remaining
Best locator for unique
element
You want to locate the unique
element on a webpage using Selenium Java. Which locator is the best choice?
ABy.tagName("footer")
BBy.id("footer")
CBy.className("footer")
DBy.xpath("//footer")
Attempts:
2 left
💡 Hint
Unique IDs are the most reliable locators.
🔧 Debug
advanced
2:00remaining
Debugging NoSuchElementException with findElement by tagName
Given the code snippet below, what is the most likely cause of a NoSuchElementException?
Selenium Java
WebElement input = driver.findElement(By.tagName("input"));
input.sendKeys("test");
AThe input element is inside an iframe not switched to.
BNo <input> element is present in the current page DOM.
CThe input element is present but disabled.
DThe input element has no 'name' attribute.
Attempts:
2 left
💡 Hint
Check if the element is inside frames or iframes.
❓ framework
expert
2:00remaining
TestNG test method using findElement by tagName
Which TestNG test method correctly finds the first

element and asserts its text equals 'Hello World'?

A
@Test
public void testParagraphText() {
  WebElement para = driver.findElement(By.tagName("p"));
  Assert.assertFalse(para.getText().isEmpty());
}
B
@Test
public void testParagraphText() {
  WebElement para = driver.findElement(By.tagName("p"));
  Assert.assertTrue(para.getText().equalsIgnoreCase("Hello World"));
}
C
@Test
public void testParagraphText() {
  WebElement para = driver.findElement(By.tagName("p"));
  Assert.assertNotNull(para.getText());
}
D
@Test
public void testParagraphText() {
  WebElement para = driver.findElement(By.tagName("p"));
  Assert.assertEquals(para.getText(), "Hello World");
}
Attempts:
2 left
💡 Hint
Use assertEquals for exact text match in TestNG.

Practice

(1/5)
1. What does the Selenium method findElement(By.tagName("button")) do?
easy
A. Finds the first <button> element on the page
B. Finds all <button> elements on the page
C. Finds the element with id 'button'
D. Finds the element with class name 'button'

Solution

  1. Step 1: Understand the method purpose

    findElement returns the first matching element found by the locator.
  2. Step 2: Analyze the locator type

    By.tagName("button") looks for elements with the tag <button>.
  3. Final Answer:

    Finds the first <button> element on the page -> Option A
  4. Quick Check:

    findElement + tagName = first matching element [OK]
Hint: findElement with tagName finds only the first matching tag [OK]
Common Mistakes:
  • Confusing findElement with findElements
  • Using tagName but expecting multiple elements
  • Mixing tagName with id or class selectors
2. Which of the following is the correct syntax to find the first <input> element using Selenium in Java?
easy
A. driver.findElement(By.tagName['input']);
B. driver.findElement(By.tagName(input));
C. driver.findElement(By.tagName("input"));
D. driver.findElement(By.tagName{input});

Solution

  1. Step 1: Check method usage

    The method findElement requires a By locator with a string argument for tagName.
  2. Step 2: Validate syntax correctness

    Only driver.findElement(By.tagName("input")); uses correct Java syntax with quotes around "input" and parentheses.
  3. Final Answer:

    driver.findElement(By.tagName("input")); -> Option C
  4. Quick Check:

    Correct method call with string tag name [OK]
Hint: Always put tag name in quotes inside By.tagName() [OK]
Common Mistakes:
  • Missing quotes around tag name
  • Using wrong brackets like [] or {}
  • Omitting parentheses after findElement
3. Given the HTML snippet:
<div>
  <h1>Title</h1>
  <h1>Subtitle</h1>
</div>

What text will be printed by this Java Selenium code?
WebElement header = driver.findElement(By.tagName("h1"));
System.out.println(header.getText());
medium
A. Subtitle
B. Title
C. TitleSubtitle
D. No text, throws exception

Solution

  1. Step 1: Understand findElement behavior

    findElement returns the first matching element, here the first <h1> which contains "Title".
  2. Step 2: Check getText output

    Calling getText() on the first <h1> returns its text content "Title" only.
  3. Final Answer:

    Title -> Option B
  4. Quick Check:

    findElement returns first tag's text [OK]
Hint: findElement by tagName returns first element only [OK]
Common Mistakes:
  • Assuming it returns all matching elements
  • Expecting concatenated text of all tags
  • Confusing findElement with findElements
4. What is wrong with this Selenium Java code snippet?
WebElement element = driver.findElement(By.tagName("button"));
System.out.println(element.getText());

Assuming the page has no <button> tags.
medium
A. It compiles but does not run
B. It returns null and prints nothing
C. It prints empty string without error
D. It throws NoSuchElementException at runtime

Solution

  1. Step 1: Check element presence

    Since no <button> tags exist, findElement cannot find any matching element.
  2. Step 2: Understand exception behavior

    findElement throws NoSuchElementException if no element is found, causing runtime error.
  3. Final Answer:

    It throws NoSuchElementException at runtime -> Option D
  4. Quick Check:

    findElement throws error if no match [OK]
Hint: findElement throws exception if tag not found [OK]
Common Mistakes:
  • Expecting null instead of exception
  • Assuming empty string returned
  • Confusing with findElements behavior
5. You want to click the first <input> element of type 'submit' on a page using Selenium Java. Which code snippet correctly finds it using findElement(By.tagName()) and then clicks it?
hard
A. WebElement submit = driver.findElement(By.tagName("input")); if ("submit".equals(submit.getAttribute("type"))) submit.click();
B. WebElement submit = driver.findElement(By.tagName("input[type='submit']")); submit.click();
C. WebElement submit = driver.findElement(By.tagName("submit")); submit.click();
D. WebElement submit = driver.findElement(By.tagName("input")); submit.click();

Solution

  1. Step 1: Understand tagName limitations

    By.tagName("input") finds the first <input> element regardless of type attribute.
  2. Step 2: Filter by type attribute manually

    Check if the found element's type attribute equals "submit" before clicking.
  3. Step 3: Analyze options

    WebElement submit = driver.findElement(By.tagName("input")); if ("submit".equals(submit.getAttribute("type"))) submit.click(); correctly finds first input, checks type, then clicks. WebElement submit = driver.findElement(By.tagName("input[type='submit']")); submit.click(); uses invalid tagName syntax. WebElement submit = driver.findElement(By.tagName("submit")); submit.click(); searches for non-existent tag <submit>. WebElement submit = driver.findElement(By.tagName("input")); submit.click(); clicks first input without type check.
  4. Final Answer:

    WebElement submit = driver.findElement(By.tagName("input")); if ("submit".equals(submit.getAttribute("type"))) submit.click(); -> Option A
  5. Quick Check:

    Use tagName then check attribute for type [OK]
Hint: findElement by tagName finds tag only; check attributes separately [OK]
Common Mistakes:
  • Trying to use CSS selector syntax inside tagName
  • Assuming tagName can filter by attribute
  • Clicking without verifying element type
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard