> 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/huong-dan-cac-action/element/count-element.md).

# Count element

Hành động này giúp tool quét trên trang web xem có bao nhiêu mục khớp với cái "tên" (XPath) mà bạn đã cung cấp, sau đó lưu con số tổng cộng đó vào một biến.

* Tác dụng: Rất hữu ích khi bạn muốn kiểm tra số lượng sản phẩm đang hiển thị, đếm số comment, hay kiểm tra xem một danh sách đã tải đủ dữ liệu chưa. Nếu đếm được 0 thì tức là không thấy mục nào, nếu đếm được nhiều hơn 0 thì bạn có thể dùng các vòng lặp để xử lý lần lượt từng mục đó.

#### Giải thích các tham số cấu hình:

* XPath: Đường dẫn mã định danh (XPath) của tập hợp các phần tử bạn muốn đếm số lượng trên giao diện trang web.
* Output variable: Tên biến số để lưu lại kết quả số lượng đếm được (Trả về một số nguyên: `0`, `1`, `2`, `3`,...).

#### Ví dụ thực tế: Đếm tổng số sản phẩm đang hiển thị trên trang cửa hàng

Khi bạn truy cập vào trang cửa hàng điện thoại như trong hình, bạn muốn kịch bản tự động kiểm tra xem trên trang hiện tại đang hiển thị bao nhiêu sản phẩm để thiết lập số vòng lặp tương tác hoặc cào dữ liệu cho chính xác:

Nhìn vào cấu trúc mã nguồn, mỗi một ô sản phẩm đều nằm chung trong một khối thẻ `div` có thuộc tính `class="card h-100"`. Bạn cấu hình hành động như sau:

* Element XPath: Nhập đường dẫn XPath chung của các ô sản phẩm: `//div[@class="card h-100"]`
* Output variable: Đặt tên biến đầu ra là `totalProducts`.

Kết quả: Hệ thống sẽ quét toàn bộ trang web và đếm xem có bao nhiêu thẻ khớp với XPath trên. Ví dụ trên màn hình hiển thị 9 sản phẩm, biến `$totalProducts` sẽ nhận giá trị là `9`.

> 💡 Ứng dụng nâng cao: Bạn có thể sử dụng biến số lượng này để truyền vào cấu hình của vòng lặp For, giúp kịch bản tự động lặp đúng `9` lần để click xem chi tiết hoặc cào đủ thông tin của cả 9 sản phẩm mà không bị thừa hay thiếu sót.

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

<figure><img src="/files/XuFX0Vdfm02Fc9hc269C" 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/huong-dan-cac-action/element/count-element.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.
