> 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/locator-feature/how-to-get-css-selectors.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# CSSセレクター / XPathを取得する

Autify でシナリオにロケータを追加する際に、どのようにCSSセレクター / XPathを指定したら良いのかわからない場合は、Google Chrome デベロッパーツール（以降デベロッパーツール）で対象要素のCSSセレクター / XPathを調べることが出来ます。

このページでは、デベロッパーツールを使って要素のCSSセレクター / XPathを取得する方法を説明します。 より安定して要素を指定する方法や、取得後に発生したエラーに関しては関連記事をご確認ください。

{% hint style="info" %}
[デベロッパーツール](https://developer.chrome.com/docs/devtools/)は、Google Chrome ブラウザに直接組み込まれたWeb開発者ツールセットです。（公式サイトより） \*Windows版とMac版でUIが若干違う箇所があります。このページではMac版を使用しています。
{% endhint %}

## デベロッパーツールの起動方法

デベロッパーツールの起動方法は下記の3通りの方法があります。

1. Chrome の右上にある「︙」アイコンから起動
   1. Chrome の右上にある **︙** アイコン をクリックします。
   2. メニュー内の「その他のツール」>**デベロッパーツール** をクリックします。

      ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-b4d3e88d7d2d646b8daeffe9f9d5c5491ac4bed3%2FChrome%20%E3%81%AE%E5%8F%B3%E4%B8%8A%E3%81%AB%E3%81%82%E3%82%8B%E3%80%8C%EF%B8%99%E3%80%8D%E3%83%A1%E3%83%8B%E3%83%A5%E3%83%BC%E3%81%8B%E3%82%89%E8%B5%B7%E5%8B%95.png?alt=media)
2. マウスの右クリックメニューから起動
   1. 対象要素の上でマウスを右クリックしてメニューを表示します。
   2. **検証** をクリックします。

      ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-0204315c174d60d290c9053f76a6546ed946a46a%2Fimage\(41\).png?alt=media)
3. ショートカットキーから起動 下記のショートカットキーを利用しても起動できます。

   ```
   Windows： Ctrl + Shift + I
   MacOS： command + option + I
   ```

   <br>

{% hint style="info" %}
デベロッパーツールの表示位置は、デベロッパーツール内右上の「︙」アイコンから開くメニューの「Dock side」で変更が出来ます。

<img src="https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-a7d381453bf9dbcf72570fd83c5beaba4f043ab8%2F%E8%A1%A8%E7%A4%BA%E4%BD%8D%E7%BD%AE%E3%81%AE%E5%A4%89%E6%9B%B4%E6%96%B9%E6%B3%95.png?alt=media" alt="" data-size="original">
{% endhint %}

## 対象要素のCSSセレクター / XPathの取得方法

今回は、[Autifyサイトトップページ](https://autify.com/ja)にある見出し、「ソフトウェアテスト自動化プラットフォームのAutifyでリリースサイクルを高速化」を例にご紹介します。

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

1. デベロッパーツールを「選択モード」にする
   1. デベロッパーツールで「Elements」タブが開いていることを確認します。
   2. デベロッパーツールの左上にあるアイコンをクリックすると「選択モード」になります。

      ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-2ca4144bc6edd95408be3169293025b05381a879%2Fimage\(43\).png?alt=media)
2. 対象要素を選択する
   1. デベロッパーツールが「選択モード」になると、サイト画面上の対象要素をマウスで選べる状態になるので、対象要素にマウスの位置を合わせクリックします。
   2. 要素に該当するソースコードが選択状態になります。

      ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-c9c69a677038a6d8b037141b08018fc4085723bd%2F%E5%AF%BE%E8%B1%A1%E8%A6%81%E7%B4%A0%E3%82%92%E9%81%B8%E6%8A%9E%E3%81%99%E3%82%8B-%E9%81%B8%E6%8A%9E%E4%B8%AD.png?alt=media)
3. 対象要素のCSSセレクター / XPathを取得する
   1. デベロッパーツールで選択状態になっているソースコード上にマウスを移動し、右クリックします。
   2. それぞれ下記操作で対象要素の値がクリップボードにコピーされます。 CSSセレクター：\[Copy] > \[Copy selector] XPath：\[Copy] > \[Copy XPath]

      ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-6a4d548e742df6c366a89858855d1f9fc9108a7d%2Fimage\(44\).png?alt=media)
   3. テキストエディターなどにコピーされた値をペーストします。

今回は以下のような値が取得できました。

* CSSセレクター

```
body > main > div:nth-child(3) > div > div > div > div.web-steps-heading.text-center > h3
```

* XPath

```
/html/body/main/div[3]/div/div/div/div[1]/h3
```

<br>

{% hint style="info" %}
CSSセレクター / XPathは、同じ要素を選ぶ場合であっても複数の指定方法があります。 自動テストにおいては、複数の指定方法がある中でも以下の点を満たすことが肝心です。

* 自分の意図を反映している
* 対象を一意に識別出来る
* 簡潔で壊れにくい（ページ構造の変化に強い）

取得したCSSセレクターの指定方法を変更する方法は、こちらの記事が参考になります。 [CSSセレクターを使いこなす](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-use-css-selectors-effectively.md)
{% endhint %}

## CSSセレクター / XPathの検証方法 <a href="#how-to-validate-css-selector-xpath" id="how-to-validate-css-selector-xpath"></a>

取得したCSSセレクター / XPathの指定方法を調整した場合などに、意図した要素を指定するものになっているかを調べたい場合は、デベロッパーツールの「Elements」パネル内で下記コマンドで検索窓を呼び出し、検索する事で確認が可能です。

```
Windows：Ctrl　+　F
macOS：command　+　F
```

CSSセレクターを例として、紹介します。（XPathも同様の方法で確認できます。） 取得したCSSセレクターを下記のように変更し、意図する要素を指定できていることを確認します。

```
変更前：body > main > div:nth-child(3) > div > div > div > div.web-steps-heading.text-center > h3
変更後：.web-steps-heading.text-center > h3
```

右下の検索窓にCSSセレクターを入力すると、要素が指定できている場合には該当要素がハイライトされます。

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

{% hint style="info" %}

* 検索にヒットしない場合には、CSSセレクターの記述内容を確認してください。
* 複数要素が検索にヒットした場合には、ロケーターが意図しない要素を特定してしまう可能性がございます。その場合は対象を一意に指定できるよう、指定方法を修正してください。
  {% endhint %}

## 関連記事

* [ロケータを追加して要素を指定する](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/add-a-locator.md)
* [CSSセレクターを使いこなす](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-use-css-selectors-effectively.md)
* [対象要素が見つかりませんでした](https://help.autify.com/nocode-web/when-the-test-doesnt-work/common-errors/element-not-found.md)


---

# 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/locator-feature/how-to-get-css-selectors.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.
