How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?
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$$()
inConsole
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:
- Press F12 to open Chrome Developer Tool
- In "Elements" panel, press Ctrl+F
- In the search box, type in XPath or CSS Selector, if elements are found, they will be highlighted in yellow.
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)
- 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. In the command line at the bottom use the following:
$()
: Returns the first element that matches. Equivalent todocument.querySelector()
or calls the$
function in the page, if it exists.$$()
: Returns an array of DOM nodes that match. This is like fordocument.querySelectorAll()
, but returns an array instead of aNodeList
.$x()
: Evaluates an XPath expression and returns an array of matching nodes.
Firefox (prior version 49)
- Install Firebug
- Install Firepath
- Press F12 to open Firebug
- Switch to
FirePath
panel - In dropdown, select XPathor CSS
- Type in to locate
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):
Chrome v69 (April 2019):
By using Chrome or Opera
without any plugins, without writing any single XPath syntax character
- right click the required element, then "inspect"
- right click on highlighted element tag, choose Copy → Copy XPath.
;)