Selenium Testing Framework Selenium Locators & Element Identification 2 — Questions and Answers
Question 1: Which XPath function is used to check if an element's attribute value contains a specific substring?
- contains() (Correct answer)
- includes()
- has()
- match()
Correct answer: contains()
The XPath contains() function checks whether a string value includes a specified substring, commonly used for dynamic or partial attribute matching.
Question 2: Which CSS selector correctly targets the first li element that is a direct child of a ul element?
- ul li:first
- ul > li:first-child (Correct answer)
- ul:first-child
- ul li[1]
Correct answer: ul > li:first-child
The selector 'ul > li:first-child' uses the child combinator (>) and the :first-child pseudo-class to select the first li that is a direct child of a ul.
Question 3: In XPath, which axis is used to select all sibling elements that appear after the current node?
- following-sibling:: (Correct answer)
- next-sibling::
- sibling::
- after-sibling::
Correct answer: following-sibling::
The 'following-sibling' axis in XPath selects all sibling elements that come after the context node in document order.
Question 4: What is the primary purpose of the By class in Selenium WebDriver?
- Creating and managing browser instances
- Providing locator strategies to find web elements (Correct answer)
- Handling browser alerts and pop-ups
- Managing browser cookies and sessions
Correct answer: Providing locator strategies to find web elements
The By class in Selenium WebDriver is a mechanism used to specify the locator strategy (such as ID, XPath, CSS, etc.) for finding web elements.
Question 5: Which Selenium method returns a collection of all elements that share the same CSS class name 'btn'?
- driver.findElement(By.className("btn"))
- driver.findElements(By.className("btn")) (Correct answer)
- driver.getElements(By.className("btn"))
- driver.selectAll(By.className("btn"))
Correct answer: driver.findElements(By.className("btn"))
driver.findElements() returns a List of WebElement objects matching the locator, while driver.findElement() returns only the first match.
Question 6: Which XPath expression correctly selects a button element whose text content is exactly 'Submit'?
- //button[@text='Submit']
- //button[.='Submit']
- //button[text()='Submit'] (Correct answer)
- //button[value='Submit']
Correct answer: //button[text()='Submit']
The XPath function text() returns the text node of an element, so text()='Submit' correctly filters elements by their visible text content.
Question 7: Which Selenium locator strategy is used to find elements by their HTML tag name, such as 'input' or 'div'?
- By.id()
- By.name()
- By.tagName() (Correct answer)
- By.className()
Correct answer: By.tagName()
By.tagName() locates elements by their HTML tag name (e.g., 'input', 'button', 'div'), which is useful when tag names are unique or when finding multiple elements of the same type.
Which XPath function is used to check if an element's attribute value contains a specific substring?