Recall & Review
beginner
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".WebElement inputField = driver.findElement(By.tagName("input"));
inputField.sendKeys("Hello");
Click to reveal answer
What does
driver.findElement(By.tagName("div")) return?✗ Incorrect
The method returns the first element matching the tag name 'div'. It does not return all elements or look for an id.
If no element with the specified tag name exists, what happens?
✗ Incorrect
Selenium throws NoSuchElementException if no matching element is found.
Which locator is generally more reliable than tag name alone?
✗ Incorrect
By.id is usually unique and more reliable than tag name or class name.
What type of value does
By.tagName() expect?✗ Incorrect
By.tagName expects a string representing the HTML tag name, like 'input' or 'button'.
Which of these is a valid use of
findElement(By.tagName())?✗ Incorrect
Only valid HTML tag names like 'input' can be used with By.tagName.
Explain how
findElement(By.tagName()) works and when you might use it in a test.Think about how you find elements by their tag in a webpage's HTML.
You got /4 concepts.
Describe the risks of using only tag name to locate elements and how to avoid them.
Consider how many buttons or inputs a page might have.
You got /4 concepts.