In Selenium Java tests, which situation best justifies choosing XPath over CSS selectors?
Think about which selector can handle text content filtering.
XPath supports selecting elements by their text content, which CSS selectors cannot do. CSS selectors are better for simple attribute or class selections.
Given the HTML snippet below, what is the number of elements matched by each locator?
<ul> <li class="item">Apple</li> <li class="item">Banana</li> <li class="item">Cherry</li> </ul>
Which option correctly states the number of elements found by these locators?
XPath: //li[contains(text(),'a')] CSS: li.item
Check which list items contain the letter 'a' in their text.
The XPath locator selects <li> elements whose text contains 'a'. 'Banana' and 'Cherry' contain 'a', so 2 elements. The CSS selector selects all <li> elements with class 'item', which are all 3.
Which assertion correctly verifies that the element located by XPath //button[@id='submit'] has the exact text 'Submit' in Selenium Java?
WebElement button = driver.findElement(By.xpath("//button[@id='submit']"));Exact text match requires equality assertion.
assertEquals checks that the button's text exactly matches 'Submit'. Using contains or assertFalse would not verify exact match. assertNull is incorrect because the text is expected.
Given the HTML below, why does the CSS selector div#main > span.highlighted fail to find the <span> element?
<div id="main">
<div>
<span class="highlighted">Text</span>
</div>
</div>Check the relationship between elements in the selector.
The '>' combinator in CSS means direct child. The <span> is inside a nested <div>, so it is a grandchild, not a direct child, causing the selector to fail.
You have a web page where element IDs change every time the page loads, but the element has a stable class and contains specific text. Which locator strategy is best to reliably find this element?
Think about how to handle dynamic IDs and stable attributes.
XPath allows combining conditions like contains() on class and text(), which helps locate elements with dynamic IDs but stable other attributes. CSS selectors cannot filter by text content.