     [Answers](https://scrapfly.io/blog)   /  [data-parsing](https://scrapfly.io/blog/tag/data-parsing)   /  [How to use XPath selectors in NodeJS when web scraping?](https://scrapfly.io/blog/answers/how-to-use-xpath-selectors-in-nodejs)   # How to use XPath selectors in NodeJS when web scraping?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Oct 31, 2022 1 min read [\#data-parsing](https://scrapfly.io/blog/tag/data-parsing) [\#nodejs](https://scrapfly.io/blog/tag/nodejs) [\#xpath](https://scrapfly.io/blog/tag/xpath) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-use-xpath-selectors-in-nodejs "Share on LinkedIn")    

 

 

CSS selectors are much more widely used in NodeJS and Javascript ecosystems though for web scraping we might need more powerful features of XPath selectors. There are few options available for XPath selectors. Most popular one in web scraping is the [osmosis](https://github.com/rchipka/node-osmosis) library:

javascript```javascript
const osmosis = require("osmosis");

const html = `
<a href="http://scrapfly.io/">link 1</a>
<a href="http://scrapfly.blog/">link 2</a>
`
osmosis
    .parse(html)
    .find('//a/@href')
    .log(console.log);
```



Another alternative is the [xmldom](https://github.com/xmldom/xmldom) library:

javascript```javascript
import xpath from 'xpath';
import { DOMParser } from '@xmldom/xmldom'

const tree = new DOMParser().parseFromString(`

    <h1>Page title</h1>
<p>some paragraph</p>
<a href="http://scrapfly.io/blog">some link</a>

`);

console.log({
    // we can extract text of the node, which returns `Text` object:
    title: xpath.select('//h1/text()', tree)[0].data,
    // or a specific attribute value, which return `Attr` object:
    url: xpath.select('//a/@href', tree)[0].value,
});
```





 

    



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 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-use-xpath-selectors-in-nodejs) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-use-xpath-selectors-in-nodejs) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-use-xpath-selectors-in-nodejs) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-use-xpath-selectors-in-nodejs) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-use-xpath-selectors-in-nodejs) 



 ## Related Articles

 [  

 http nodejs 

### Web Scraping With NodeJS and Javascript

In this article we'll take a look at scraping using Javascript through NodeJS. We'll cover common web scraping libraries...

 

 ](https://scrapfly.io/blog/posts/web-scraping-with-nodejs) [  

 data-parsing css-selectors 

### Parsing HTML with CSS Selectors

Introduction to using CSS selectors to parse web-scraped content. Best practices, available tools and common challenges ...

 

 ](https://scrapfly.io/blog/posts/parsing-html-with-css) [  

 data-parsing xpath 

### Ultimate XPath Cheatsheet for HTML Parsing in Web Scraping

Ultimate companion for HTML parsing using XPath selectors. This cheatsheet contains all syntax explanations with interac...

 

 ](https://scrapfly.io/blog/posts/xpath-cheatsheet) 

  ## Related Questions

- [ Q How to use CSS selectors in NodeJS when web scraping? ](https://scrapfly.io/blog/answers/how-to-use-css-selectors-in-nodejs)
- [ Q How to select all elements between two elements in XPath? ](https://scrapfly.io/blog/answers/how-to-select-all-elements-between-two-known-elements-in-xpath)
- [ Q How to use XPath selectors in Python? ](https://scrapfly.io/blog/answers/how-to-use-xpath-selectors-in-python)
- [ Q Can I used XPath selectors in BeautifulSoup? ](https://scrapfly.io/blog/answers/can-i-use-xpath-selectors-in-beautifulsoup)
 
  



   



 Extract structured data with AI, **1,000 free credits** [Start Free](https://scrapfly.io/register)