0
0
Selenium Javatesting~5 mins

findElement by tagName in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe first <div> element on the page
BAll <div> elements on the page
CThe element with id 'div'
DAn error because 'div' is not a valid locator
If no element with the specified tag name exists, what happens?
AReturns an empty list
BReturns null
CThrows NoSuchElementException
DReturns the body element
Which locator is generally more reliable than tag name alone?
ABy.className
BBy.tagName
CBy.xpath with absolute path
DBy.id
What type of value does By.tagName() expect?
ACSS selector
BHTML tag name as a string
CXPath expression
DElement text
Which of these is a valid use of findElement(By.tagName())?
Adriver.findElement(By.tagName("input"))
Bdriver.findElement(By.tagName("submit"))
Cdriver.findElement(By.tagName("#input"))
Ddriver.findElement(By.tagName(".button"))
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.