How to find Xpath of a webpage element



XPath is a very valuable tool for scraping data from a website or XML data source however you might ask what is the best way to generate an Xpath to get the value of an element on a page.

How to generate XPath


Without having to jump into a website's source code and try to decipher the HTML structure, there is a much easier way to generate and Xpath for an element.

The answer is by using a browser extension, there is a great extension for Google Chrome which will allow you to load up any website and generate an Xpath for any element on the page.

The extension I'm going to show you is only available in Google Chrome, however if you use another browser like Mozilla Firefox there are many other similar extensions available that can generate an Xpath for you.

Install Xpath Generator Chrome Extension

  1. Open Google Chrome and navigate to https://chrome.google.com/webstore/detail/xpath-generator/dphfifdfpfabhbkghlmnkkdghbmocfeb
  2. Click "Add to Chrome"
  3. The extension will download and install and you should see the following icon in the Chrome menu bar.

How to use Xpath Generator


  1. Navigate to a website with some data you would like to retrieve
  2. In the Chrome menu bar click on the Xpath generator icon
  3. A window will pop up, click on "Enable the Generator UI on this page"
  4. To demonstrate how xpath generator works I have search Google for Sydney Harbour bridge, we will use Xpath generator to get the length of the Sydney Harbour bridge.
  5. On your page, hover your mouse pointer over the desired element and press ALT+A on your keyboard.
  6. You should see the Xpath generator pane update with a number of xpath possibilities, you should aim to use the Xpath with the lowest number of matches.
  7. Click on the number of matches next to the Xpath, your selected Xpath will be populateed in the textbox above allowing you to easily copy the path.
  8. That's it now you can implement your Xpath into your code or spreadsheet data.

Was this helpful?

Yes No


Comments