How to select elements by ID using CSS selectors?

To select elements by ID value the # syntax can be used. For example, #product would select any element that has product in its ID attribute like <div id="product"></div> element:

<div> <div id="product">select</div> <div id="sold product">select</div> <div id="sold product new">select</div> <div id="product-2">ignore</div> </div>

An important note here ID selector matches any element that contains the value in the ID list (separated by spaces). To match elements that has an exact ID value the = predicate can be used instead:

<div> <div id="product">ignore</div> <div id="sold product">select</div> <div id="sold product new">ignore</div> <div id="product-2">ignore</div> </div>

Provided by Scrapfly

This knowledgebase is provided by Scrapfly — a web scraping API that allows you to scrape any website without getting blocked and implements a dozens of other web scraping conveniences. Check us out 👇