🚀 We are hiring! See open positions

How to get the name of an HTML element in XPath?

by Bernardas Alisauskas May 29, 2023 1 min read

To get the name of the selected element in XPath the name() function can be used. This function returns the name of the closest element in the current context.

The name() function is useful in HTML parsing in broad selection cases like selecting multiple names. See this interactive example:

<!-- select all links by selecting the @href attributes --> <article> <p>paragraph 1</p> <a>link 1</a> <p>paragraph 2</p> <script>some script data</script> <p>paragraph 3</p> </article>

Above, we're selecting only paragraph and link elements by using wildcard match (the * symbol) and a name() matching predicate.

Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.