2

I am trying to learn xpath extensively i need to have bit of practical approach, every tutorials on youtube suggests to install firebug, xpath checkers add ons in mozilla firefox but unfortunately ff is not supporting it as a beginner please suggest me the way to learn this hands on.

tried downloading older versions still there is no addons or plugins available

JagaSrik
  • 700
  • 7
  • 23

1 Answers1

7

Here is the simple approach that will work in all browsers.

Using the console: works in all browsers

You can execute any xpath using "$x" in the browser console as shown below. Here is the general notation $x("xpath goes here"), this will return the array so if you want to get the first matching element then you have to do something like this $x("xpath goes here")[0] as shown in the below image.

enter image description here

Using Developer Tools in Chrome:

If you want to use chrome then you can use the developer tools > elements tab for xpath and css validation besides console.

enter image description here

Press 'F12' when you are in chrome to open the developer tools, then select "Eelements" tab. Now press 'Ctrl+F' and enter the xpath in the search box at the bottom, you will see the number of matching elements count at the right end of the search box as shown in the screenshot.

For Beginners:

As suggested by @BillHillerman in a comment below: If you are beginner does not know who to write the xpath then you can relay on the xpath that will be provided by chrome browser. Right click on element that you are interested in then click on Inspect. Now right click on the html code of the element Copy > Copy Xpath. Now you have the xpath in the clipboard for you.

Chrome provided xpath: enter image description here

Ken White
  • 123,280
  • 14
  • 225
  • 444
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • 1
    To add to this answer, if you want to see Google's suggested xpath for whatever element you highlight, right-click on the highlighted HTML code, select copy, then copy xpath. Personally I like to craft my own xpath most of the time, but the suggested xpath is quite often sufficient. – Bill Hileman Apr 26 '19 at 17:31
  • @BillHileman Thanks for your point. Updated the answer with your suggestion. Ken Thanks for the Edit. – supputuri Apr 27 '19 at 00:40