A CSS selector is a pattern for selecting HTML elements from your web pages. It allows you to use content from the selected HTML elements in conjunction with AddSearch’s features, such as AddSearch Recommend, Custom Fields Manager, and Text Extraction Rules tool.
More specifically:
The following instructions will guide you in finding the CSS selector for specific HTML elements on your web pages. Identifying a CSS selector involves locating the HTML element on a page and using the Inspector tool, which is available in most modern browsers such as:
In the example, we aim to identify the CSS selector for the price element on the page.
Google Chrome displays the CSS selector at the bottom of the Developer Tools panel. You can also find the selector on the page by hovering over the highlighted element’s code.
In the example, we aim to identify the CSS selector for the price element on the page.
Safari displays the CSS selector at the top of the Web Inspector panel under the tabs. You can also view the CSS selector on the page by hovering over the highlighted element’s code.
In the example, we aim to identify the CSS selector for the price element on the page.
Firefox displays the CSS selector at the bottom of the Inspector panel. You can also view the CSS selector on the page by hovering over the highlighted element’s code.
You can test the CSS selector in the browser’s Console tab. To access this tab, right-click on the webpage with the element and select Inspect in Google Chrome and Firefox, or Inspect Element in Safari. This will open the Inspector panel. Then, click on the Console tab within this panel to open the Console and it’s command line.
In the Console’s command line, type document.querySelector('CSS SELECTOR')
, replacing ‘CSS SELECTOR’ with the selector you have identified from the web page and hit Enter. If the CSS selector is correct, it will return the HTML element in the Console.
For instance, for the product’s price (identified in the examples above), if you type document.querySelector('#productPrice')
, the console should return <p id="productPrice">$29.99</p>
.
We’re always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team.