Selenium Testing Framework Selenium Locators & Element Identification 1 — Questions and Answers
Question 1: Which locator strategy is generally considered the most reliable and fastest for finding elements in Selenium WebDriver?
- XPath
- CSS Selector
- ID (Correct answer)
- Class Name
Correct answer: ID
ID is the most reliable locator because IDs should be unique within an HTML page, making element identification fast and unambiguous.
Question 2: Which XPath expression correctly selects all input elements with a type attribute equal to 'text'?
- //input[@type='text'] (Correct answer)
- //input[type='text']
- input[@type='text']
- /input[@type='text']
Correct answer: //input[@type='text']
The correct XPath syntax uses double-slash to search anywhere in the document and square brackets with @ to specify attribute conditions.
Question 3: What is the key behavioral difference between findElement() and findElements() in Selenium WebDriver?
- findElement() returns a list; findElements() returns a single element
- findElement() throws NoSuchElementException if not found; findElements() returns an empty list (Correct answer)
- findElement() is always faster than findElements()
- findElements() only works with XPath locators
Correct answer: findElement() throws NoSuchElementException if not found; findElements() returns an empty list
findElement() throws NoSuchElementException when no match is found, while findElements() safely returns an empty list, allowing null checks without exception handling.
Question 4: Which CSS selector correctly targets an element with the ID 'login-btn'?
- .login-btn
- #login-btn (Correct answer)
- *login-btn
- @login-btn
Correct answer: #login-btn
In CSS selectors, the # symbol is used to target elements by their ID attribute, making #login-btn the correct syntax.
Question 5: What does the '//' at the beginning of an XPath expression signify?
- Select only the root element
- Select elements anywhere in the document (Correct answer)
- Select only direct children of the root
- Select elements that have no children
Correct answer: Select elements anywhere in the document
The double-slash '//' is a shorthand for the 'descendant-or-self' axis, meaning it searches for matching elements anywhere in the entire document.
Question 6: Which type of XPath is considered LEAST reliable for test automation due to fragility?
- XPath using ID attribute
- XPath using name attribute
- Absolute XPath from the root (Correct answer)
- XPath using contains()
Correct answer: Absolute XPath from the root
Absolute XPath (starting with a single /) is fragile because any structural change in the DOM will break the locator, making it unsuitable for stable automation.
Question 7: Which Selenium method is used to locate an anchor element by a portion of its visible link text?
- By.linkText()
- By.partialLinkText() (Correct answer)
- By.text()
- By.link()
Correct answer: By.partialLinkText()
By.partialLinkText() matches anchor elements whose visible text contains the specified substring, unlike By.linkText() which requires an exact match.
Which locator strategy is generally considered the most reliable and fastest for finding elements in Selenium WebDriver?