# Check an XPath Button Match Before Using It in Selenium

> A two-button HTML sample shows why a valid XPath still needs a one-element check before a Selenium click.

- Author: [Shashank Rawlani](https://shashank.rawlani.com)

- Published: 2026-11-22T05:30:00.000Z

- Updated: 2026-11-22T05:30:00.000Z

- Category: Tools & Reviews

- Canonical source: https://automationtester.in/blog/tools-reviews/check-xpath-button-match-before-selenium

- Tags: xpath, selenium, locators

Will your XPath select Pay alone, or Pay and Cancel? Paste the HTML into the [XPath Tester](https://automationtester.in/tools/xpath-tester) and read the matching nodes before putting the locator in Selenium.

## Start with two similar controls

Paste this reduced HTML into the tool:

```
<section>
  <button class="action">Pay</button>
  <button class="action">Cancel</button>
</section>
```

Enter `//button[text()="Pay"]` in the XPath field. The live page reports **1 match**: a `<button>` with `class="action"` and text `Pay`. That matters more than whether the expression is valid. A broad `//button` would also include Cancel, and the test might click the wrong control after a layout change.

The tool uses the browser's `DOMParser` followed by `document.evaluate()`. It shows attributes and a short HTML snippet for each node, making a quick count check possible without adding a temporary Selenium test. The evaluator also accepts scalar XPath expressions. For example, `count(//button)` returns a number rather than a list of elements; use a node-selecting expression when you need to inspect which button matched.

## Move the check into the real page

Once the pasted fragment selects one node, verify the locator in your actual browser test:

```
import { By } from 'selenium-webdriver';

const pay = driver.findElement(By.xpath('//button[text()="Pay"]'));
await pay.click();
```

Replace `driver` with your Selenium WebDriver instance. The pasted fragment has no application state: it cannot show a hidden mobile duplicate, a button inserted after hydration, or a shadow-root control. Read the rendered DOM when those are possible. The tool also caps displayed node matches at 200, so a count of 200 is not proof that only 200 elements exist.

## The CSS suggestion is a hint

The page can suggest CSS for simple XPath shapes such as an ID or attribute match. Treat that as a starting point. The converter covers a small set of patterns; it does not translate every XPath axis or predicate. Its CSS suggestion cannot express the `text()="Pay"` condition in this example, so keep the XPath or use a locator with an accessible name in the live test.

Use a stable ID or test attribute when the product supplies one. If the test's goal is to interact as a user, a role and accessible name may be clearer in frameworks that support those locators. Keep XPath for cases where DOM relationships are the actual contract, then assert that the locator is unique on the live page.

## Sources

- [AutomationTester.in XPath Tester](https://automationtester.in/tools/xpath-tester)

- [MDN: `Document.evaluate()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate)

## Continue reading on AutomationTester.in

- [Reject a Playwright Locator That Matches Two Buttons](https://automationtester.in/blog/tools-reviews/reject-duplicate-playwright-locator-suggestions)
- [Triage a Playwright Strict Mode Error Before Editing the Locator](https://automationtester.in/blog/tools-reviews/triage-playwright-strict-mode-before-editing-locator)
- [Debug Appium Selector Failures That Differ on Android and iOS](https://automationtester.in/blog/automation-tutorials/debug-appium-selectors-android-ios)
- [Debug Playwright Strict-Mode Locator Failures](https://automationtester.in/blog/automation-tutorials/debug-playwright-strict-mode-locator-failures)
- [Read a Cron Expression's Day Fields in Its Next Runs](https://automationtester.in/blog/tools-reviews/read-cron-day-field-or-in-next-runs)

Source: [Check an XPath Button Match Before Using It in Selenium](https://automationtester.in/blog/tools-reviews/check-xpath-button-match-before-selenium) by Shashank Rawlani.
