What does the findElement(By.tagName()) method do in Selenium?
It locates the first web element on the page that matches the specified HTML tag name, such as div, input, or button.
Click to reveal answer
beginner
How do you use findElement(By.tagName()) to find a button element?
You call driver.findElement(By.tagName("button")) to find the first <button> element on the page.
Click to reveal answer
intermediate
What happens if findElement(By.tagName()) does not find any matching element?
It throws a NoSuchElementException, meaning the element with that tag name does not exist on the current page.
Click to reveal answer
intermediate
Why is it better to use more specific locators than just tag name in Selenium tests?
Because many elements share the same tag name, using only tag name can find the wrong element or cause flaky tests. More specific locators like id or class reduce errors.
Click to reveal answer
beginner
Write a simple Java code snippet using Selenium to find the first input element and enter text "Hello".
findElement returns the first matching element, here the first <h1> which contains "Title".
Step 2: Check getText output
Calling getText() on the first <h1> returns its text content "Title" only.
Final Answer:
Title -> Option B
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
Step 1: Check element presence
Since no <button> tags exist, findElement cannot find any matching element.
Step 2: Understand exception behavior
findElement throws NoSuchElementException if no element is found, causing runtime error.
Final Answer:
It throws NoSuchElementException at runtime -> Option D
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
Step 1: Understand tagName limitations
By.tagName("input") finds the first <input> element regardless of type attribute.
Step 2: Filter by type attribute manually
Check if the found element's type attribute equals "submit" before clicking.
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.
Final Answer:
WebElement submit = driver.findElement(By.tagName("input")); if ("submit".equals(submit.getAttribute("type"))) submit.click(); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.