How to find xpath of an element in firefox inspector

104,332

Solution 1

The bug related to losing ability to 'copy XPath' from firefox inspector is fixed, verified in Firefox 56 beta, and verified in Firefox 57 alpha: https://bugzilla.mozilla.org/show_bug.cgi?id=987877

Solution 2

You can use the console to check if the xpath you want return the correct element or not. $x("//div/xpath") https://developer.mozilla.org/en-US/docs/Tools/Settings

Solution 3

Follow Bellow Steps:

Step 1 : Right click on page -> Select (Inspect Element)

Step 2 : Pick an element from the page

Step 3 : Right Click on highlighted html -> Copy -> Xpath

enter image description here

Solution 4

In Firefox you can use the web developer tools console for xpath validation like this:

1.Open Web Developer tools.

2.Click on Console

3.Type $x("path")

This should let you validate that your path is valid.

Solution 5

You can't, firefox's inspector does not have such feature. However it does offer css selector which can be converted to xpath with various other tools.

It should be noted however that these generated selectors (xpath or css) are not accurate or reliable and you should avoid using this feature for anything but rare edge cases.

Share:
104,332
papakias
Author by

papakias

Quack!

Updated on July 09, 2022

Comments

  • papakias
    papakias almost 2 years

    In Firefox 50.1, Firebug is no longer available so I have to use the inspector but I cannot find copy ---> xpath option that was available in Firebug. How can I find the xpath of an element using the inspector?

  • papakias
    papakias over 7 years
    So I suppose I should download a plugin?
  • Granitosaurus
    Granitosaurus over 7 years
    There are definitely a lot of plugins/extensions that offer that, yes. However FIrebug still works with Firefox v50.1 so you are free to use that.
  • papakias
    papakias over 7 years
    I don't think it still works. When I press the firebug button, the inspector opens. This happens since I updated to firefox 50.1
  • papakias
    papakias over 7 years
    Thank you for your answer. I was hoping for a black box approach.. Suppose that I don't have access to the code.
  • mosaad
    mosaad over 7 years
    this is in the browser.
  • papakias
    papakias over 7 years
    Can you describe how to do this a little bit more then?
  • mosaad
    mosaad over 7 years
    you open developer options and one of the tabs is the console where you can write the command i wrote and it will return an element that you can check if it is the one that you want
  • Zelphir Kaltstahl
    Zelphir Kaltstahl almost 7 years
    The OP is not asking, what they should learn, but how to do it with existing means in Firefox. I think this answer is off-topic.
  • clarkep
    clarkep almost 7 years
    why aren't xpaths reliable?
  • Granitosaurus
    Granitosaurus almost 7 years
    @pjc xpaths are very reliable but automatically generated xpaths usually tend to be very stationary and based on DOM structure rather than unqiue node classes or ids. If dom structure changes you will lose your output, however if you were to select by unique classes or ids, your spider will manage to find the data even if big structural changes occur.
  • clarkep
    clarkep almost 7 years
    Gotcha. It seems like the xpaths from the chrome inspector are ok, however.
  • papakias
    papakias over 6 years
    And how do I copy the xpath after the fix?
  • Morte Descovich
    Morte Descovich over 6 years
    per the bugfix description, a firefox web developer / inspector node context menu item "Copy XPath" is added in those beta/alpha versions.
  • papakias
    papakias over 6 years
    After Updating to firefox 56 I can confirm the availability of the copy xpath option!
  • Morte Descovich
    Morte Descovich over 6 years
    same here, Firefox 56.0 Menu Tools->Web Developer->Inspector, right click an element to open menu, Copy->Xpath
  • mrtipale
    mrtipale over 6 years
    but its really hard to form and test large complex xpaths. need dedicated extn like firebug+firepath .
  • papakias
    papakias over 6 years
    Thanks for your anser Pavan but I don't want to verify an xpath. I want to find the xpath of an element whose xpath is unknown to me.
  • Monkey Supersonic
    Monkey Supersonic over 6 years
    This sucks with namespace prefixes in general XML documents.
  • mosaad
    mosaad over 6 years
    @MonkeySupersonic I don't understand. Can you give an example ?
  • Monkey Supersonic
    Monkey Supersonic over 6 years
    <?xml version="1.0" ?><p:SomeElement xmlns:p="anyURL" /> and $x("//p:SomeElement") though FF is not the first choice for an XML parser anyway.
  • becixb
    becixb about 6 years
    @Zelphir I don't understand. It is basically similar to the answer of mosaad. Once you know how to construct the xpath, then you can use the browser console to test the xpath. AFAIK, getting the xpath in Firefox was not available back when the question was asked. It was only available with Firebug. The only way to get the xpath is to actually know how to construct one and test it in the console.
  • malhal
    malhal about 4 years
    Just copies the single element ID not the full path