0
0
Selenium Javatesting~5 mins

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

Choose your learning style9 modes available
Recall & Review
beginner
What does findElement(By.xpath()) do in Selenium?
It locates the first web element on the page that matches the given XPath expression.
Click to reveal answer
beginner
Write a simple XPath to find a button with the text 'Submit'.
XPath: //button[text()='Submit'] finds a button element whose visible text is 'Submit'.
Click to reveal answer
intermediate
Why is XPath useful compared to other locators like ID or class name?
XPath can locate elements based on complex conditions like text, attributes, or hierarchy, even if IDs or classes are missing or dynamic.
Click to reveal answer
beginner
What happens if findElement(By.xpath()) does not find any matching element?
It throws a NoSuchElementException, causing the test to fail unless handled.
Click to reveal answer
beginner
How to write an XPath to find an input element with attribute name='email'?
XPath: //input[@name='email'] selects an input element with the attribute name equal to 'email'.
Click to reveal answer
What does driver.findElement(By.xpath("//div[@id='main']")) do?
AFinds all <div> elements with id 'main'
BFinds the element with class 'main'
CFinds the element with tag 'main'
DFinds the first <div> element with id 'main'
Which exception is thrown if findElement(By.xpath()) finds no element?
ANoSuchElementException
BElementNotVisibleException
CTimeoutException
DNullPointerException
Which XPath finds a button with text 'Login'?
A//button[text()='Login']
B//button[@text='Login']
C//button[contains(text(),'Login')]
D//button[@value='Login']
What is the best practice for XPath locators?
AUse absolute XPath starting from root
BUse relative XPath with unique attributes
CUse XPath with index only
DUse XPath with random attributes
Which XPath selects all <input> elements with type 'checkbox'?
A//input[type='checkbox']
B//checkbox
C//input[@type='checkbox']
D//input[@checkbox='true']
Explain how to use findElement(By.xpath()) to locate a web element and what happens if the element is not found.
Think about how Selenium searches the page and error handling.
You got /3 concepts.
    Describe best practices for writing XPath expressions for Selenium tests.
    Consider what makes locators reliable and easy to update.
    You got /4 concepts.