> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-en/action-guides/element/count-elements.md).

# Count element

This action helps the tool scan the website to see how many items match the "name" (XPath) you provided, and then store that total number in a variable.

* Effect: Very useful when you want to check the number of products displayed, count the number of comments, or check if a list has loaded enough data. If the count is 0, it means no items were found; if the count is greater than 0, you can use loops to process each item sequentially.

#### Explanation of configuration parameters:

* XPath: The XPath identifier path of the set of elements you want to count on the web interface.
* Output variable: The variable name to store the counted result (Returns an integer: `0`, `1`, `2`, `3`,...).

#### Real-world example: Count the total number of products displayed on the store page

When you access the mobile phone store page as shown in the image, you want the script to automatically check how many products are currently displayed on the page to set the number of interaction loops or scrape data accurately:

Looking at the source code structure, each product box is contained within a `div` tag with the attribute `class="card h-100"`. You configure the action as follows:

* Element XPath: Enter the common XPath path of the product boxes: `//div[@class="card h-100"]`
* Output variable: Name the output variable `totalProducts`.

Result: The system will scan the entire website and count how many tags match the above XPath. For example, if 9 products are displayed on the screen, the variable `$totalProducts` will receive the value `9`.

> 💡 Advanced application: You can use this count variable to pass into the configuration of the For loop, allowing the script to automatically iterate exactly `9` times to click for details or scrape enough information for all 9 products without excess or omission.

<figure><img src="/files/UkIMh5QwuyzPtqNBd2MQ" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/viTcY6s2vjWfTocxgpZh" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.gpmautomate.com/automate-en/action-guides/element/count-elements.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
