     [Answers](https://scrapfly.io/blog)   /  [xpath](https://scrapfly.io/blog/tag/xpath)   /  [How to select elements by ID in XPath?](https://scrapfly.io/blog/answers/how-to-select-elements-by-id-in-xpath)   # How to select elements by ID in XPath?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Apr 19, 2023 1 min read [\#xpath](https://scrapfly.io/blog/tag/xpath) 

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

 

 

To select elements by element ID using XPath we can match the `@id` attribute with the `=` operator or the `contains()` function.

For example, to select `<a id="home"></a>` element we could use `//a[@id="home"]` or `//a[contains(@id, "home")]` selectors as seen in these interactive examples:

&lt;html&gt; &lt;a id="ignore"&gt;&lt;/a&gt; &lt;a id="home"&gt;website&lt;/a&gt; &lt;a id="ignore2"&gt;website 2&lt;/a&gt; &lt;/html&gt; For dynamic IDs we can use `contains()` to match elements by partial ID:

&lt;html&gt; &lt;a id="ignore"&gt;&lt;/a&gt; &lt;a id="home-231"&gt;website&lt;/a&gt; &lt;a id="ignore2"&gt;website 2&lt;/a&gt; &lt;/html&gt; Note that by CSS design there should only be one element of a given ID on the page which makes selecting elements by ID a very fast and reliable method.



 

   Table of Contents















 

   Join the Newsletter  Get monthly web scraping insights 

 

  



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-select-elements-by-id-in-xpath) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-elements-by-id-in-xpath) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-elements-by-id-in-xpath) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-elements-by-id-in-xpath) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-elements-by-id-in-xpath) 



 ## Related Articles

 [  

 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) [  

 python data-parsing 

### Parsing HTML with Xpath

Introduction to xpath in the context of web-scraping. How to extract data from HTML documents using xpath, best practice...

 

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

 headless-browser puppeteer 

### How to find elements by XPath in Puppeteer?

To find elements by XPath using Puppeteer the "$x()" method can be used which will execute XPath selection on the curren...

 

 ](https://scrapfly.io/blog/posts/how-to-find-elements-by-xpath-in-puppeteer) 

  ## Related Questions

- [ Q How to select elements by class in XPath? ](https://scrapfly.io/blog/answers/how-to-select-elements-by-class-in-xpath)
- [ Q How to select elements by attribute value in XPath? ](https://scrapfly.io/blog/answers/how-to-select-elements-by-attribute-value)
- [ Q How to select elements of a specific position in XPath? ](https://scrapfly.io/blog/answers/how-to-select-elements-of-specific-position-in-xpath)
- [ Q How to find HTML elements by attribute using BeautifulSoup? ](https://scrapfly.io/blog/answers/how-to-find-html-elements-by-attribute-with-beautifulsoup)
 
  



   



 Scale your web scraping effortlessly, **1,000 free credits** [Start Free](https://scrapfly.io/register)