> 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/when-the-test-doesnt-work/tips/how-to-fix-css-selector-errors.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# CSSセレクターエラーの解決方法

このページでは、CSSセレクターの間違いが原因で発生する「要素が見つかりませんでした」エラーの解決方法を説明します。 前提として、以下の条件を満たすものとします。

* CSSとHTMLを読み書きできる
* Chrome DevToolsを使ってHTML要素を検証できる

（分からない場合は[関連記事](https://help.autify.com/nocode-web/when-the-test-doesnt-work/tips/how-to-fix-css-selector-errors.md)をご覧ください）

## よくある原因と解決方法

まず、現在使用しているCSSセレクターを確認しましょう。

失敗したステップをクリックすると、CSSセレクターを確認できます。

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

では、よくある原因とその解決方法を見ていきましょう。

### 1. 自動生成されたCSSセレクター

#### 原因

以下の例のように、CSSセレクターが非常に長い場合、DevToolsによって自動生成されたものである可能性があります。

```
#header-navigation > div > nav > div > div.d-none.d-lg-block.flex-grow-1.h-100 > div > div > div:nth-child(3) > a
```

CSSセレクターはDevToolsから手軽に取得できますが、ウェブサイトの構造が頻繁に更新される場合は壊れやすいので注意が必要です。

#### 解決方法

対象要素固有の`id`、あるいは`class`の組み合わせを探しましょう。

例として、以下の2つが挙げられます。

```
a#link_to_login_page
```

```
header a.navigation.login
```

`id`または`class`がない場合、下記のように属性セレクターを使って他の属性を指定することもできます。

```
a[href="./login"]
```

```
a img[alt="login button"]
```

こうすることで、サイトの更新に強いセレクターを取得できます。

### 2. ランダムなidやclass名

下の例のように、CSSセレクターにランダムな文字列が含まれる場合、idまたはclass名が何らかのビルドツールによって生成された可能性があります。

```
a.login_ac2187y3
```

ランダムな部分（先頭、末尾、あるいはclass名・id全体）は、ビルド後に変更される可能性があります。

このようなケースも、属性セレクターを使って解決できます。

```
a[href="./login"]
```

また、一貫性のある部分がある場合（上の例では `login`）、ワイルドカード構文を使って指定できます。

下は、「login」という文字列を含むclass名と一致します。

```
a[class*="login"]
```

下は、「login」で始まるclass名と一致します。

```
a[class^="login"]
```

属性セレクターについて詳しくは[MDN Web Docs - Attribute selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors)をご覧ください。

## Tips

### CSS セレクターの一意性を確認する方法

CSSセレクターがページ上の1つの要素にしかマッチしないことを確認するには、下記の手順に従ってください。

1. DevToolsウィンドウを開きます。 2.「要素」タブを開きます。
2. Ctrl + F（Windows）または Cmd + F（Mac）を押して、検索テキストボックスを開きます。
3. テキストボックスにCSSセレクターを入力します。
4. セレクターが検出した要素の数を確認します。

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

## それでも解決できない場合...

お気軽にカスタマーサクセスチームにご連絡ください。解決のお手伝いをいたします🙂

## 関連記事

* CSSセレクターの基本は、[CSSセレクターを使いこなす](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-use-css-selectors-effectively.md)をご覧ください。
* HTML要素を検証する方法は、[ロケータを追加して要素を指定する](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/add-a-locator.md)をご覧ください。[Chrome DevTools](https://developer.chrome.com/docs/devtools/overview/)の公式ドキュメントも参考になります。
* CSSセレクターで使用できる高度な構文については、[MDN Web Docs - Attribute selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors)をご覧ください。


---

# 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/when-the-test-doesnt-work/tips/how-to-fix-css-selector-errors.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.
