How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?

300,904

Solution 1

Chrome

This can be achieved by three different approaches (see my blog article here for more details):

  • Search in Elements panel like below
  • Execute $x() and $$() in Console panel, as shown in Lawrence's answer
  • Third party extensions (not really necessary in most of the cases, could be an overkill)

Here is how you search XPath in Elements panel:

  1. Press F12 to open Chrome Developer Tool
  2. In "Elements" panel, press Ctrl+F
  3. In the search box, type in XPath or CSS Selector, if elements are found, they will be highlighted in yellow.

enter image description here

Firefox (since version 75)

Since FF 75 it's possible to use raw xpath query without evaluation xpath expressions, see documentation for more info.

Firefox (prior version 75)

  1. Either select "Web Console" from the Web Developer submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on Mac OS X)
    or press the Ctrl+Shift+K (Command+Option+K on OS X) keyboard shortcut.
  2. In the command line at the bottom use the following:

    • $(): Returns the first element that matches. Equivalent to document.querySelector() or calls the $ function in the page, if it exists.

    • $$(): Returns an array of DOM nodes that match. This is like for document.querySelectorAll(), but returns an array instead of a NodeList.

    • $x(): Evaluates an XPath expression and returns an array of matching nodes.


Firefox (prior version 49)

  1. Install Firebug
  2. Install Firepath
  3. Press F12 to open Firebug
  4. Switch to FirePath panel
  5. In dropdown, select XPathor CSS
  6. Type in to locate

enter image description here

Solution 2

You can open the DevTools in Chrome with CTRL+I on Windows (or CMD+I Mac), and Firefox with F12, then select the Console tab), and check the XPath by typing $x("your_xpath_here").
This will return an array of matched values. If it is empty, you know there is no match on the page.

Firefox v66 (April 2019):

Firefox v66 console xpath

Chrome v69 (April 2019):

Chrome v69 console xpath

Solution 3

By using Chrome or Opera

without any plugins, without writing any single XPath syntax character

  1. right click the required element, then "inspect"
  2. right click on highlighted element tag, choose CopyCopy XPath.

;)

Enter image description here

Solution 4

Another option to check your xpath is to use selenium IDE.

  1. Install Firefox Selenium IDE
  2. Open your application in FireFox and open IDE
  3. In IDE, on a new line, paste your xpath to the target and click Find. The corresponding element would be highlighted in your application

Selenium IDE

Share:
300,904

Related videos on Youtube

user3448242
Author by

user3448242

Updated on November 09, 2020

Comments

  • user3448242
    user3448242 over 3 years

    How can I verify my XPath?

    I am using Chrome Developers tool to inspect the elements and form my XPath. I verify it using the Chrome plugin XPath Checker, however it does not always give me the result. What is a better way to verify my XPath.

    I have also tried using Firebug to inspect the bug and also using the FirePath to verify. But does Firepath also verify the XPath.

    My last option would be to use the Selenium WebDriver to confirm my XPath.

  • Jens Erat
    Jens Erat about 10 years
    Just one warning on XPath and browsers, as this leads to lots of confusion: stackoverflow.com/questions/18241029/…
  • Yi Zeng
    Yi Zeng about 10 years
    @JensErat: Really good point. For others reading this comment, as already mentioned in that question, Selenium is not affected because it drives browsers and uses the same underlying JavaScript evaluation technology with dev tools.
  • djangofan
    djangofan about 10 years
    This method isn't ideal but it is good to know because whatever xpath expression you write in that console can be executed by a JavascriptExecutor in Selenium. Sometimes you can use that as a workaround, perhaps when Firefox native events are disabled?
  • Arran
    Arran about 10 years
    @djangofan, how do you mean? By that logic both of these answers, although correct, are unnecessary by your logic.
  • JacekM
    JacekM about 10 years
    @user1177636: What software did you use for generating these GIFs?
  • tuxErrante
    tuxErrante over 7 years
    If you need a local parser for XQuery too I've found BaseX: basex.org/products
  • Automationtested
    Automationtested almost 7 years
    For Chrome, this is the BEST solution I have seen yet. Thanks for sharing. Makes it easier to confirm in Chrome when I don't wish to use Firefox.
  • ouflak
    ouflak almost 7 years
    I have used this to great benefit. But the one big problem is that sometimes the context menu is disabled or overwritten. Then you have to rely on the other approaches.
  • derloopkat
    derloopkat over 6 years
    Answer needs an update for Firefox. Unfortunately Firebug is obsolete and has been merged into Developer Tools. Currently you can test xpath expression in Console, e.g. $("//div")
  • Major Major
    Major Major about 5 years
    This doesn't really answer the question. It isn't verifying an existing xpath, it's generating one. The one that Chrome generates is often ugly and very fragile when a much better solution exists.
  • YasserKhalil
    YasserKhalil almost 5 years
    @bosnjak when trying this in console $x("//input[@name='q']") I got VM251:1 Uncaught TypeError: $x is not a function at <anonymous>:1:1
  • bosnjak
    bosnjak almost 5 years
    @YasserKhalil which browser are you using, which version? What site are you doing this on?
  • Treer
    Treer over 4 years
    derloopkat's answer is the best for me, but it's missing an "x" - it should be $x("//div"). XPath expressions can be tested in the console with $x("expr") and css expressions with $$("expr")
  • Peter Mortensen
    Peter Mortensen over 3 years
    The link is broken (404).