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:
- Choose Elements tab
- Press Element Selector button
- With that button pressed you click an element in the main browser window.
- In the DOM-elements window you right-click that highlighted element.
- The context menu appears, and you choose Copy.
- Choose Copy XPath in a sub menu. Now you have that element’s XPath in a console buffer.
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.
Note what xpath the Chrome browser has issued for Trailing P/E element at a Yahoo finance page: