0
0
Selenium Javatesting~20 mins

Choosing XPath vs CSS strategy in Selenium Java - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath vs CSS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to prefer XPath over CSS selectors?

In Selenium Java tests, which situation best justifies choosing XPath over CSS selectors?

AWhen you want to select elements using simple tag names.
BWhen you want to select elements by ID attribute.
CWhen you need to select elements based on their text content.
DWhen you want to select elements by class name only.
Attempts:
2 left
💡 Hint

Think about which selector can handle text content filtering.

Predict Output
intermediate
2:00remaining
Output of locating elements with XPath vs CSS

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
AXPath finds 2 elements; CSS finds 3 elements.
BXPath finds 3 elements; CSS finds 2 elements.
CXPath finds 1 element; CSS finds 3 elements.
DXPath finds 3 elements; CSS finds 3 elements.
Attempts:
2 left
💡 Hint

Check which list items contain the letter 'a' in their text.

assertion
advanced
2:00remaining
Correct assertion for element text using XPath

Which assertion correctly verifies that the element located by XPath //button[@id='submit'] has the exact text 'Submit' in Selenium Java?

Selenium Java
WebElement button = driver.findElement(By.xpath("//button[@id='submit']"));
AassertNull(button.getText());
BassertTrue(button.getText().contains("Submit"));
CassertFalse(button.getText().equals("Submit"));
DassertEquals("Submit", button.getText());
Attempts:
2 left
💡 Hint

Exact text match requires equality assertion.

🔧 Debug
advanced
2:00remaining
Why does this CSS selector fail to locate the element?

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>
ABecause the &lt;span&gt; is not a direct child of &lt;div id="main"&gt; but a grandchild.
BBecause the &lt;span&gt; does not have the class 'highlighted'.
CBecause the CSS selector syntax is invalid.
DBecause the &lt;div&gt; with id 'main' does not exist.
Attempts:
2 left
💡 Hint

Check the relationship between elements in the selector.

framework
expert
2:00remaining
Choosing locator strategy for dynamic elements in Selenium Java

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?

AUse CSS selector with ID attribute.
BUse XPath with contains() to match the stable class and text content.
CUse XPath with exact ID match.
DUse CSS selector with tag name only.
Attempts:
2 left
💡 Hint

Think about how to handle dynamic IDs and stable attributes.