> 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-mobile/troubleshooting/tips/tips-to-stabilize-element-selection-xpath-and-accessibility-id.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# 要素選択を安定させるTips (XPathとAccessibility ID)

画像認識で要素選択が安定しない場合は、XPathかAccessibility IDを利用することで安定して要素を選択できる可能性があります。ここでは、具体的なXPathやAccessibility IDの利用方法をお伝えします。

## XPath <a href="#xpath" id="xpath"></a>

ロケータにXPathを選択した場合、Autifyは自動でXPathを生成します。

自動生成されたXPathは要素の種類と表示されている順番のみを使用しますが、手がかりとなる情報追加したり余分な情報を削除することで安定性を向上させることができます。

例:

| 種類       | 値の例                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | 特徴                                                                                      |
| -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| 自動生成     | /XCUIElementTypeApplication/XCUIElementTypeWindow\[1]/XCUIElementTypeOther/XCUIElementTypeOther/<>XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell\[2]/XCUIElementTypeOther\[1]/XCUIElementTypeOther/XCUIElementTypeSecureTextField | 要素のツリー全体を参照するため、要素と直接関係ない変更が影響することがある。要素の種類と順番にのみ注目するため、同じ種類の要素で順番が入れ替わると間違った要素を選択してしまう |
| 手動による調整後 | //XCUIElementTypeCell\[contains(@value, “Password”)]//XCUIElementTypeSecureTextField                                                                                                                                                                                                                                                                                                                                                                                                    | 選択したい要素にとって重要な情報だけを取り入れることができ、安定性が向上する。学習にはある程度の試行錯誤が必要                                 |

以下によく使われるXPathの例を示します。

### 属性に関わるもの

クイック要素更新画面で、対象の要素やその親子要素に特徴的な属性がついていた場合、それをXPathに含めることで安定性を向上させることができます。ここではvalue属性を利用する例を紹介します。

| XPath                                 | 指定する要素                       |
| ------------------------------------- | ---------------------------- |
| //XCUIElementTypeButton\[@value="OK"] | valueが "OK" であるボタン           |
| //\*\[contains(@name, "限定")]          | nameに "限定" を含む要素             |
| //\*\[starts-with(@name, "2024")]     | nameが "2024" で始まる要素          |
| //\*\[ends-with(@name,"ログイン")]        | nameが "ログイン" で終わる要素          |
| //\*\[@name="Return"]                 | nameが "Return" である要素         |
| (//\*\[@name="Done" or "完了"])         | nameが "Done" もしくは "完了" である要素 |

### 順番に関わるもの

XPathにマッチする要素が複数ある際に、任意の順番の要素を選択することができます

| XPath                               | 指定する要素       |
| ----------------------------------- | ------------ |
| //XCUIElementTypeButton\[3]         | 3番目のボタン      |
| //XCUIElementTypeTextField\[last()] | 最後のテキストフィールド |

### 複合的なロケータ

選択したい要素そのものに特徴が少ない場合、他の要素との相対的な関係で要素を選択することができます。

| XPath                                             | 指定する要素                            |
| ------------------------------------------------- | --------------------------------- |
| //\*\[@value=”登録”]//XCUIElementTypeButton         | valueが “登録” である要素の中にあるボタン         |
| //\*\[value=”Check out”] \~ XCUIElementTypeButton | valueが “Check out” である要素の並びにあるボタン |
| //\*\[@name=”List”]/ \*\[5]                       | nameが “List” である要素の直下にある要素の5番目    |

## Accessibility ID

他の手法で安定的に要素が選択できない場合は、Accessibility IDの設定をお勧めします。

クイック要素更新の画面で、対象要素に iOSの場合は “name” 属性、 Androidの場合は “content-desc” 属性が設定されていた場合、Accessibility IDに属性の値を設定することで対象の要素が選択できます。

対象要素がAccessibility IDを持っていなかった場合、アプリケーションのコードを変更する必要があります。

### Accessibility IDの設定方法

### iOS ネイティブ

対象要素に [accessibilityIdentifier](https://developer.apple.com/documentation/uikit/uiaccessibilityidentification/1623132-accessibilityidentifier) 属性を追加します。

### Android ネイティブ

対象要素に contentDescription を追加します。

### Flutter

(iOS)対象要素に[Semanticsのidentifier属性](https://api.flutter.dev/flutter/semantics/SemanticsData/identifier.html)を設定します。

(Android) 現時点ではご利用いただけません。要素に対してSemanticsのidentifier属性を設定すると、Page source中では “resource-id” という属性として表示されるので、XPath中で使用できます。

### ReactNative

(iOS)対象要素に [testID 属性](https://reactnative.dev/docs/next/view#testid)を設定します。

(Android) 現時点ではご利用いただけません。要素に対してtestID属性を設定すると、Page source中では “resource-id” という属性として表示されるので、XPath中で使用できます。


---

# 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-mobile/troubleshooting/tips/tips-to-stabilize-element-selection-xpath-and-accessibility-id.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.
