> For the complete documentation index, see [llms.txt](https://help.autify.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/js-steps-for-elements.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# 要素に対するJSステップ

JSステップでは、以下のような手順でなんらかの操作を実現する場面がよくあります。

1. 要素を選択する
2. 選択された要素に対する操作を行う

従来は、常にJSステップ内で `querySelector()` などを使用して要素を選択する必要がありましたが、もう一つの方法、**画面から要素を選択してJSステップの引数として渡す** 方法が取れるようになりました。 この方法を用いると、もはやCSSセレクターやXPathを書かずともJSステップで取り扱う要素を選択できるようになります！

使い方は以下の通りです。

1. Autify Recorderでレコーディングを開始します。
2. 対象要素を含むページに移動します。
3. 対象の要素を右クリック（Macの場合はCtl + Click）します。
4. コンテキストメニューから **JSステップを追加する** をクリックします。

   ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-706cf1c664f36a73f4495073c4b3ead8d55f1ee6%2FScreen%20Shot%202022-08-17%20at%206.29.18.png?alt=media)
5. Autify Recorderで `保存` をクリックします。

すると新しいJSステップが作成され、引数に `element` が指定されます。これがレコーディング中に選択された要素で、この `element` に対して行いたい操作を通常のJavaScriptで実装することができます。

次の例では、選択された要素に対してダブルクリックをしています。要素を選択するためのコードが不要になっていることにお気づきでしょうか？

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-e99395c201caa44bd5bb0ab92a609dd4b5dc012c%2FScreen%20Shot%202022-08-17%20at%206.37.08.png?alt=media)

要素に対するJSステップの利点の1つは、**サイト構造が多少変化しても、AIが要素探索を行うため、対象の要素を見つけることができる** 点です。これは従来、指定したCSSセレクタやXPathと完全に一致する要素がなくなった場合に、常にJSステップを更新する必要があったのとは対照的です。これにより、JSステップのメンテナンスコストを大幅に削減することができます！

{% hint style="warning" %}
**注意**

* モバイルサイズ(375x812)でレコーディングしている際に、要素に対するJSステップを追加するには、対象の要素を一度クリックすることでフォーカスをし、そのあとで要素に対するJSステップを追加する必要があります。この時不要なクリックステップがレコーディングされますので、このステップは削除してください。
  * ローカルリプレイ後すぐに、対象の要素をクリックせずに要素に対するJSステップを追加し保存した場合、シナリオにステップが追加されません。
  * 別の要素をクリックした後に、対象の要素をクリックせずに要素に対するJSステップを追加すると、別の要素に対するJSステップが追加されます。
* 常にN番目の要素を指定したいなど、AIの探索に頼らずに特定の条件のもとで要素を選択したい場合には、要素に対するJSステップは不向きです。引き続き、CSSセレクターやXPathで要素を指定するとよいでしょう。
  {% endhint %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.autify.com/nocode-web/autify-university/advanced/js-steps/js-steps-for-elements.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
