> 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/tips-1/select-box-tips/select-the-nth-of-options.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# リストから選択肢のN番目を選択する

## リストから選択肢のN番目を選択するには

[リストから値を選択するステップ](https://help.autify.com/nocode-web/autify-university/foundation/creating-a-scenario/select-from-list-by-label.md)は、ラベルをもとに選択肢を選択するため、「選択肢のN番目を選択する」といった使い方ができません。

しかし、「[要素に対するJSステップ](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/js-steps-for-elements.md)」と「リストから値を選択する」ステップを組み合わせることで「選択肢のN番目を選択する」操作を実現可能です。

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

今回紹介する手法は、`<select>`要素以外で実装されたリストに対しては使えませんのでご注意ください。
{% endhint %}

### 要素に対するJSステップを使用する利点

通常のJSステップを使用することで、一つのJSステップでリストから選択肢のN番目を選択することも可能ですが、「要素に対するJSステップ」を使うことで得られる以下の利点を考え、今回は「要素に対するJSステップ」と「リストから値を選択する」ステップを組み合わせる方法を紹介します。

* AutifyのAIが要素を取得するのでCSSセレクター/XPathの取得を省ける。
* 要素取得の処理を書かなくて良いので比較的少ないコードで済む。
* ページのローディング完了や要素の出現を自動で待ってくれる。

### レコーディングとシナリオの編集

レコーディング中に対象のリストを右クリックし、**Autify Recorder > JSステップを追加する** を選択します。次にリストから適当な値を選択し **保存** をクリックします。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-b1a20588e368ed9898edc1137d1e72ebc3f797e2%2Fja_select_option_n_1.png?alt=media)

すると「JavaScriptのコードを実行する（以下、要素に対するJSステップ）」と「リストから〜を選択する」ステップの2つが作成されます。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-e27a5128acfff00cb811caeb847b1969c72a9003%2Fja_select_option_n_2.png?alt=media)

「要素に対するJSステップ」を開き、以下のコードを貼り付けます。要素はAutifyが取得してくれるので、比較的短いコード量になります。

```javascript
// リストは0から始まるので4番目の選択肢を選択したい場合は3を指定します。
const index = 3;

// -- ここから下は変える必要はありません --
// 指定したindex番号を持つラベルを取得します。
const label = element.options[index].label;

// 取得したラベルをreturnし、後続のステップで使用できるようにします。
return label;
```

下記の部分で選択肢のindex番号を指定しています。0から始まるため、4番目の選択肢を指定したい場合は、3を指定します。

```javascript
const index = 3;
```

次に、「リストから〜を選択する」ステップをクリックし、ラベルの値を、「他のステップから取得」に変更し、「要素に対するJSステップ」のステップを指定し保存します。これで「要素に対するJSステップ」が取得したラベルを選択するようになります。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-6e1c9e099e5b3424edd9f6222aef2c69ef1f4620%2Fja_select_option_n_3.png?alt=media)

### テスト実行

テストを実行してみます。

実行結果を見ると、「要素に対するJSステップ」のreturnの値が期待するラベルの値であることを確認できました。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-e05e84b49e40e705f8d0a76ac81a45915bfe856e%2Fja_select_option_n_4.png?alt=media)

リストの値もreturnした値に変更されていることが確認できました。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-da40650b3665469ca90b06215ee1e4a2c57fd4f0%2Fja_select_option_n_5.png?alt=media)


---

# 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/tips-1/select-box-tips/select-the-nth-of-options.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.
