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?
10 Answers
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
-
Thank you for your answer. I was hoping for a black box approach.. Suppose that I don't have access to the code.– papakiasCommented Jan 26, 2017 at 17:38
-
4
-
-
2you 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– mosaadCommented Jan 27, 2017 at 10:56
-
2This sucks with namespace prefixes in general XML documents. Commented Jan 24, 2018 at 15:30
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
-
2
-
1I 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
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
-
2
-
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!– papakiasCommented Sep 28, 2017 at 19:38
-
5same 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 .– mrtipaleCommented Nov 23, 2017 at 12:31
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.
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.
-
-
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– papakiasCommented Jan 26, 2017 at 11:58
-
1
-
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
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.
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.
Naviagte to console tab
On the console editor we can verify our Xpath
- For verifying xpath we have to use our xpath in following format : $x("xpath")
-
1Thanks 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.– papakiasCommented Jan 7, 2018 at 21:38
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
-
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.– thelogixCommented Dec 15, 2022 at 10:24
I know that this doesn't answer directly to the question but it helped me a lot, use Pale Moon:
- Open any web page in Pale Moon browser
- Right click on an element of the page
- Select: "Inspect Element with Developer Tools" (a window shows up with element highlighted)
- Right click highlighted element
- Select: "Copy XPath"
And there you have it. You will get a "full" xpath even if the element has an id.
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.
-
5The 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.– becixbCommented May 11, 2018 at 7:58