How to find xpath of an element in firefox inspector
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
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.
Comments
-
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 over 7 yearsSo I suppose I should download a plugin?
-
Granitosaurus over 7 yearsThere 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 over 7 yearsI 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 over 7 yearsThank you for your answer. I was hoping for a black box approach.. Suppose that I don't have access to the code.
-
mosaad over 7 yearsthis is in the browser.
-
papakias over 7 yearsCan you describe how to do this a little bit more then?
-
mosaad over 7 yearsyou 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 almost 7 yearsThe 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 almost 7 yearswhy aren't xpaths reliable?
-
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 almost 7 yearsGotcha. It seems like the xpaths from the chrome inspector are ok, however.
-
papakias over 6 yearsAnd how do I copy the xpath after the fix?
-
Morte Descovich over 6 yearsper the bugfix description, a firefox web developer / inspector node context menu item "Copy XPath" is added in those beta/alpha versions.
-
papakias over 6 yearsAfter Updating to firefox 56 I can confirm the availability of the copy xpath option!
-
Morte Descovich over 6 yearssame here, Firefox 56.0 Menu Tools->Web Developer->Inspector, right click an element to open menu, Copy->Xpath
-
mrtipale over 6 yearsbut its really hard to form and test large complex xpaths. need dedicated extn like firebug+firepath .
-
papakias over 6 yearsThanks 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 over 6 yearsThis sucks with namespace prefixes in general XML documents.
-
mosaad over 6 years@MonkeySupersonic I don't understand. Can you give an example ?
-
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 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 about 4 yearsJust copies the single element ID not the full path