> 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/faq/faq-create-scenario/what-if-i-want-to-click-on-the.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# テーブルやリストで意図した要素を操作するにはどうしたらいいですか？

シナリオ作成時にテーブルの一番上の要素に対する操作をレコーディングし、その要素が「test0001」というテキストを持っているとします。しかしある時新しいレコードが追加され、二番目に「test0001」が移動し、一番目に「test0002」が新しく表示されている場合は、Autifyは二番目にある「test0001」の要素を対象にしてしまうなど、意図した要素を操作できない可能性があります。

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

この場合、常に一番上にある最新のレコードをクリックする前提のシナリオでは、後続のステップでテストが失敗することが考えられます。

現状テーブルやリストで、一番上やN番目、一番下など意図した要素を確実に対象としたい場合は、[ロケータ機能](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/add-a-locator.md)を使う必要があります。

以下ロケータに設定するCSSセレクター/XPathの例を、いくつか紹介します。なお **本記事で紹介しているCSSセレクター/XPathはあくまで例** ですので、実際のサイト構造を確認し、適切なCSSセレクター/XPathを作成してください。

## CSSセレクターでN番目の要素を対象にする

CSSセレクターにおいてN番目の要素を対象にしたい場合は、以下の構文が使えます。

* N番目

```css
:nth-child(N)
```

* 最初

```css
:first-child
```

* 最後

```css
:last-child
```

もしもリストの2番目を対象にしたい場合は以下のようになります。

```css
ul > li:nth-child(2)
```

## テーブルのN行目・M列目にある要素を対象にする

テーブルの場合は行と列を指定する必要があります。`tr`タグが行、`td`タグが列を表すので、それぞれに何番目の要素を取得したいかを指定する必要があります。以下は1行目の1列目にある要素を取得するCSSセレクターです。最新のデータが一番上に来るテーブルの場合、このCSSセレクターは常に最新のデータが対象になります。

```css
table > tbody > tr:first-child > td:first-child
```

以下のテーブルの場合赤枠で囲まれた要素が対象になります。

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

上述赤枠の対象要素はtd要素ですが、この要素の下に`a`タグで作られたリンクが存在します。このリンクをクリックしたい場合はCSSセレクターの末尾に `a` を追加します。

```css
table > tbody > tr:first-child > td:first-child a
```

もちろん最終列も対象にできます。その場合は以下のように`td:first-child`を`td:last-child`に変更します。

```css
table > tbody > tr:first-child > td:last-child
```

## テーブルで特定のテキストを持つ行の特定の列を対象にする

N行目・M列目のような指定ではなく、あるテキストを持つ要素が存在する行の、特定の列を対象にしたいケースもあるかと思います。例えばAutifyのシナリオ一覧テーブルから、シナリオ名が「test0001」の行の最後の要素を対象にしたい場合は以下のXPathを使用します。

```
//table/tbody/tr[td//*[text()='test0001']]/td[6]
```

緑枠で囲まれたtd要素が対象になります。

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

対象となった要素はtd要素ですが、この要素の下にはbuttonタグで囲まれたメニューアイコンが存在します。このbuttonタグを対象にしたい場合は、以下のようにXPathを変更します。

```
//table/tbody/tr[td//*[text()='test0001']]/td[6]//button
```

もしも検索したいテキストが毎回同じではなく、テスト実行ごとに[JSステップ](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/javascript-steps.md)や[動的な値](https://help.autify.com/nocode-web/autify-university/foundation/creating-a-scenario/dynamic-value-generation.md)を使ってランダムに作成している場合は、JSステップを使用する必要があります。引数として別のステップで作成した検索したい文字列を受け取り、以下のJSスニペットの中で使用してください。

* [テーブル内をテキストで検索し見つかった行の特定の列を操作する](https://javascript-snippets.autify.com/ja/snippets/others/search_in_table_and_operate_specific_column/)


---

# 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/faq/faq-create-scenario/what-if-i-want-to-click-on-the.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.
