Skip to main content

How to find the XPath of your Selenium WebDriver test using Chrome browser?

How to find the XPath of your Selenium WebDriver test using Chrome browser?

Key takeaways

  • Chrome DevTools can copy an XPath, but the generated one is usually position-based and brittle.
  • Rewrite it around an id or a stable attribute before putting it in a test.
  • If no stable hook exists, adding one to the page is cheaper than maintaining the locator.

A lot of times while developing a test or when debugging a test you want to check to what element the XPath used in your test refers to. You will find this useful since you don’t need to install any other plugin or widget, you can use just built-in browser functionality.

For example, let’s imagine we have the following code in our UI Selenium test:


@FindBy(xpath = "//div[contains(@class,'item--lowest')]//a[contains(@class,'summary__date')]")
public WebElement summaryDateElement;

To identify to which element this refers to just do the following:

    1. Open the URL / web page in the Chrome browser
    2. Open the Web developer tools by pressing:
      •  Cmd + Alt + I (on Mac)
      • or by clicking View -> Developer -> Developer tools
      • or by Right-Click and Inspect Element

See more details

[emaillocker]

  1. Click on the Console tab in the Web developer tools
  2. Paste in the console the XPath from your test in the following format:
  3. $x(“//div[contains(@class,’item–lowest’)]//a[contains(@class,’summary__date’)]”)
  4. Hovering with the mouse over the returned result will highlight the page element that the XPATH refers to.
  5. Details about locating elements with Selenium WebDriver can be found in our previous blog post.

That is it. You will see in the console what the XPath returns.

Find best screenshot comparison tools

[/emaillocker]

Frequently Asked Questions

How do I test an XPath in Chrome without a plugin?

Open DevTools, go to the Console and evaluate $x(“your-xpath”). It returns the matching nodes, and hovering over a result highlights the element on the page, so you can see immediately whether the locator points where you think it does.

Should I use the XPath that Chrome copies for me?

Rarely as is. Copy as XPath produces a position based path such as a chain of div indexes, which breaks as soon as anyone inserts an element. Rewrite it around an id or a stable attribute before it goes into a test.

What if the element has no stable attribute?

Adding one to the application is usually cheaper than maintaining a brittle locator. A single data-testid costs the developer a minute and saves the test from every future layout change.

Chris

Head of Content at LoadFocus

Chris leads content and oversees development across the services this blog covers. The guides and tool comparisons here come out of the same decisions that shape what ships.

Weekly tips right in your inbox.

Don’t miss out on the latest testing tips and tricks, tools and news related to software testing.

Already +20,000 members in the list.

The Load Testing Platform with AI That Explains Your Results

Load test websites and APIs from 25 cloud regions, monitor page speed and uptime, and get AI analysis that explains your results in plain English.

Cloud load testing, website speed and API monitoring in one place.

Hi there! Can I share a cool thing I'm working on with you?

A software testing newsletter that sparks joy.

Our goal is to create helpful content for software developers and testers.

If you're not into it, you can unsubscribe instantly.