53

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?

1
  • 1
    it is still available, just not maintained
    – eLRuLL
    Commented Jan 25, 2017 at 17:21

10 Answers 10

46

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

7
  • Thank you for your answer. I was hoping for a black box approach.. Suppose that I don't have access to the code.
    – papakias
    Commented Jan 26, 2017 at 17:38
  • 4
    this is in the browser.
    – mosaad
    Commented Jan 27, 2017 at 8:03
  • Can you describe how to do this a little bit more then?
    – papakias
    Commented Jan 27, 2017 at 9:48
  • 2
    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
    – mosaad
    Commented Jan 27, 2017 at 10:56
  • 2
    This sucks with namespace prefixes in general XML documents. Commented Jan 24, 2018 at 15:30
41

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

2
  • 2
    Just copies the single element ID not the full path
    – malhal
    Commented Mar 17, 2020 at 17:28
  • 1
    I do not know if the inspector has been updated since the post by @malhal in 2020, but when I tried this solution, I got the full XPath from /html to the specific element. As far as I can tell, the solution proposed by @NanheKumar works. The currently selected "best answer" is incorrect, as the post itself does not answer the question, though it is quite helpful. This is the correct answer. Commented Mar 6 at 16:54
13

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

5
  • 2
    And how do I copy the xpath after the fix?
    – papakias
    Commented Sep 18, 2017 at 7:20
  • per the bugfix description, a firefox web developer / inspector node context menu item "Copy XPath" is added in those beta/alpha versions. Commented Sep 20, 2017 at 4:42
  • After Updating to firefox 56 I can confirm the availability of the copy xpath option!
    – papakias
    Commented Sep 28, 2017 at 19:38
  • 5
    same here, Firefox 56.0 Menu Tools->Web Developer->Inspector, right click an element to open menu, Copy->Xpath Commented Sep 30, 2017 at 14:46
  • but its really hard to form and test large complex xpaths. need dedicated extn like firebug+firepath .
    – mrtipale
    Commented Nov 23, 2017 at 12:31
11

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.

0
3

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.

6
  • So I suppose I should download a plugin?
    – papakias
    Commented Jan 25, 2017 at 21:46
  • 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. Commented Jan 25, 2017 at 23:24
  • 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
    Commented Jan 26, 2017 at 11:58
  • 1
    why aren't xpaths reliable?
    – clarkep
    Commented Aug 6, 2017 at 18:23
  • 1
    @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. Commented Aug 7, 2017 at 5:50
2

Unfortunately this doesn't work properly. When I use the copy xpath, I got this instead of the usual one: //*[@id="gwt-uid-105"] Which is just useless :(

There aren't any extension currently which could solve this. Looks like the only way is to run an old version of FF. Can have an old 32 bit and a new 64 bit version.

0

Xpath Using Firefox Console: **
Answer referenced from **Xpath Using Firefox Console in selenium webdriver

Steps:

Press F12( common for all browsers), Now firefox open developer tool like below.

  1. Naviagte to console tab

  2. On the console editor we can verify our Xpath

  3. For verifying xpath we have to use our xpath in following format : $x("xpath")
1
  • 1
    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.
    – papakias
    Commented Jan 7, 2018 at 21:38
0

This is an old question but I'm glad to say that since FF 75 it supports searching by XPath, look at documentation. Here is a link to their blog entry

1
  • The question asked, is how to copy the XPath of an element. Not how to search by XPath. Your links do not seem to address this.
    – thelogix
    Commented Dec 15, 2022 at 10:24
-1

I know that this doesn't answer directly to the question but it helped me a lot, use Pale Moon:

  1. Open any web page in Pale Moon browser
  2. Right click on an element of the page
  3. Select: "Inspect Element with Developer Tools" (a window shows up with element highlighted)
  4. Right click highlighted element
  5. Select: "Copy XPath"

And there you have it. You will get a "full" xpath even if the element has an id.

-8

I would rather you study how to create xpath on your own to select the element you need. It's very simple and very very helpful specially when creating reusable methods/functions.

Go and see the xpath tutorial at w3schools or wherever.

Focus on the relationships between elements.

Edit: Once you know how to construct a good-looking xpath, you can now test it using the browser console as mentioned by @mosaad. Still, you need to learn how to construct the xpath first. BTW, the copy xpath function wasn't available back in Jan '17. That is why I suggested he learn how to construct xpath. Even now that it is available though, I still suggest you learn because the xpath you get from those usually suck.

2
  • 5
    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. Commented Aug 3, 2017 at 15:39
  • @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.
    – becixb
    Commented May 11, 2018 at 7:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.