Locate a button with a generated id
intermediate XPath Challenge 2 of 13
On the checkout page, the Submit order button has an id such as btn-8f3a2c, which is regenerated on every build. The starter worked yesterday and fails today. Write a locator that finds the button by its visible text instead.
Requirements
- Use
driver.findElement(By.xpath("...")). - Do not use the generated id.
- Use
normalize-space()(orcontains()) to cope with the whitespace and icon inside the button. - Resolve to the “Submit order” button, not “Submit”, “Submit order feedback” or the help link.
Expected output
Resolves to the "Submit order" button in the review section, whatever its generated id is.
Checks (4)
- ○Resolves to exactly the intended element(s)
- ○Uses By.xpath
- ○Does not hard-code the generated id
- ○Matches text robustly with normalize-space() or contains()
Solution.javaLocator engine · not a real Playwright run
Ctrl/⌘ + Enter to check locator · your code is saved in this browser
Results
Check your locator to see which elements it resolves to.
Page under test
matches are outlined in greenView page HTML
<main class="checkout">
<form class="search" role="search">
<input name="q" type="search" aria-label="Search">
<button type="submit" id="btn-1c9e04">Submit</button>
</form>
<section class="review">
<h2>Review your order</h2>
<p>3 items, total $142.80</p>
<a href="/help/orders" class="help">Help with submitting an order</a>
<button type="button" id="btn-77ab1f" class="btn secondary">Save for later</button>
<button type="submit" id="btn-8f3a2c" class="btn primary">
<span class="icon icon-lock"></span>
Submit order
</button>
</section>
<section class="feedback">
<p>How was checkout today?</p>
<button type="button" id="btn-d41e9a">Submit order feedback</button>
</section>
</main>