Categories
Development

Find XPath using web developer tools

Often for the purpose of scraping, one needs to find certain elements’ XPath on a webpage. How can one do that with browser Web developer tools, aka Web inspector? A picture is worth of thousand words.

In the Chrome browser web dev. tools (F12) you execute the following steps:

  1. Choose Elements tab
  2. Press Element Selector button
  3. With that button pressed you click an element in the main browser window.
  4. In the DOM-elements window you right-click that highlighted element.
  5. The context menu appears, and you choose Copy.
  6. Choose Copy XPath in a sub menu. Now you have that element’s XPath in a console buffer.

Attention!

The browser composes an element’s XPath based on its own algorithm. It might not be the way you think or the way that fits to your code. So, you have to understand XPath in nature, be acquainted with it.

For example the Chrome browser has issued for Trailing P/E element at a Yahoo finance page the following xpath:

//*[@id=”main-0-Quote-Proxy”]/section/div2/section/div/section/div2/div1/div1/div/table/tbody/tr[3]/td1/span

2 replies on “Find XPath using web developer tools”

That is a great tip. So far, it has worked perfectly for me while working on a C#.NET app. The only thing I had to do was escape quotes in the string.

Sample directly from Chrome Copy Xpath://*[@id=”login-loginForm”]/p/button/span
After adding Escape for quotes: //*[@id=\”login-loginForm\”]/p/button/span

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.