# Working with XPaths

[Video: Working with XPaths](https://www.youtube.com/embed/9GFv26NyAkk)

XPaths are expressions that identify elements on a web page. For example, `//img` selects all `<img>` elements.

If you are more familiar with CSS selectors than XPath, refer to this [CSS-to-XPath conversion table](https://hakre.wordpress.com/2012/03/18/css-selector-to-xpath-conversion/).

The table below lists common XPath patterns you can adapt for your own site:

| XPath                                        | Description                                                                                                                                      |
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `//h1`                                       | Selects all `<h1>` elements.                                                                                                                     |
| `//div[@id="main"]`                          | Selects the `<div id="main">` element. Useful for restricting indexing to a specific page region and excluding footers or sidebars.              |
| `//p[contains(@class,"notes")]`              | Selects `<p>` elements whose class includes `notes`, e.g. `<p class="something notes whatever">`.                                                |
| `//img[contains(@class,"main-image")]//@src` | Selects the `src` attribute of `<img>` elements whose class includes `main-image`. Use this to tell the crawler which image to index for a page. |

***

## Control what content is indexed

The SS360 crawler automatically identifies titles, images, and content for your search results. When the defaults do not match your site's structure, you can fine-tune indexing by pointing the crawler to specific content blocks or excluding unwanted ones.

Configure indexing rules in two places:

* **[Crawler settings](https://app.sitesearch360.com/data/import/crawling)** — site-wide rules
* **[Result Groups](https://app.sitesearch360.com/data-structuring/result-grouping)** — rules scoped to individual result groups (if you use result grouping)

### Define XPaths with XPath Helper

1. Install the [XPath Helper](https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en) Chrome extension.

2. Go to a page on your website and click the XPath Helper icon in the browser toolbar to open the black overlay panel.

3. Hold **Shift** and hover over elements on the page. The extension highlights each element in yellow and shows the corresponding XPath in the **QUERY** field. The **RESULTS** field previews the matched content.

   ![xpath](/assets/images/image.1624289596602-b8b5c70765c32720489f8e5a0c37b207.png)

4. Shorten the generated XPath to make it more general. Two common approaches:

   * Remove from the **end** to match more child nodes.
   * Remove from the **front** to broaden the match — when shortening from the front, the expression must start with `//`.

   A good shortening target is any element that has an `id` attribute. You can drop everything before it and start the XPath there.

   **Example:**

   ```
   /html/body[@id='body']/main/section[@class='u-pb-xxl u-pb-xl--sm']/article[@class='flex container'][1]/div[@class='main-feature__content col-6 col-12-sm']
   ```

   Shortened to:

   ```
   //*[@id='body']//div[contains(@class,'main-feature__content')]
   ```

5. Copy the XPath into the SS360 control panel under [Data Structuring → Content Extraction](https://app.sitesearch360.com/data-structuring/content-extraction) and paste it into the appropriate field (see the next section for field descriptions).

6. Click **Test** and enter a page URL to verify the result. You can also use [Index Single URL](https://app.sitesearch360.com/data/list/content) to preview everything that will be extracted from a page at once.

***

## Content Extraction XPath fields

Configure all fields under [Data Structuring → Content Extraction](https://app.sitesearch360.com/data-structuring/content-extraction). Enter one XPath per line.

### Title XPaths

Points the crawler to the main title of each page. The default is `//h1`.

Other common values:

* `//title` — uses the HTML `<title>` tag
* `//h2` — useful when pages do not have an `<h1>`

**Title Regular Expression** gives you additional control over the extracted title string. For example, if every page title includes a brand suffix:

```
<title>Working with XPaths – Site Search 360</title>
```

Set **Title XPath** to `//title` and **Regular Expression** to `([^–])+`. The crawler then uses only the part before the `–`, dropping `– Site Search 360`.

![Data structuring](/assets/images/image.1622052878474-4e42123b9456e75ae5ed76efe7ecb781.png)

### Image XPaths

Points the crawler to the main image on a page. Matched images are shown as thumbnails in search results.

Leave this field empty if the crawler's default image detection works for your site. To target a specific image:

```
//img[@id='main']/@src
```

For lazy-loaded images, target the `data-src` attribute instead:

```
//div[@class='product-detail-images']//img/@data-src
```

To prevent any images from being indexed, toggle **Extract Images** off. Alt texts and captions can still be indexed separately.

### Default Image XPath

Specifies a fallback image to use when no other image is found on a page. Example:

```
//img[@id='logo']/@src
```

### Include Content XPaths

Restricts indexing to specific content blocks. Leave empty to index everything.

### Exclude Content XPaths

Tells the crawler to ignore specific content blocks. Leave empty to index everything.

### Search Snippet XPath

Located under [Search Settings → Search Snippet → Use content behind search snippet XPath](https://app.sitesearch360.com/search/settings/generic).

By default, SS360 shows content surrounding the matched search terms. A common alternative is to use page meta descriptions instead. The field is pre-filled with:

```
//meta[@name="description"]/@content
```

To switch to meta descriptions, go to [Search Settings](https://app.sitesearch360.com/search/settings/generic) and update the **Search Snippet Source**.

![search snippet](/assets/images/image.1622054521070-198e92fcb8c5add22d3074158e58f568.png)

***

## Further reading

* [XPath introduction — W3Schools](https://www.w3schools.com/xml/xpath_intro.asp)
* [CSS selector to XPath conversion](https://hakre.wordpress.com/2012/03/18/css-selector-to-xpath-conversion/)
