> 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/common-errors/javascript-error.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# JavaScriptでエラーが発生しました

JSステップでエラーが発生した場合、テスト結果には以下のメッセージが表示されます。

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

「JavaScriptでエラーが発生しました: 」の後のメッセージは、JSステップのどこに問題があるかを探るヒントになります。

以下JSステップのエラーについて考えられる原因と、その対応方法をいくつか紹介します。

## ページの読み込み完了前にJSステップが実行された

JSステップはページの読み込みを待たずに実行されます。たとえばCSSセレクターで取得した要素に対してクリックを実行するJSステップの場合、ページの読み込み完了前に実行されると要素が正しく取得できずに失敗してしまいます。

テスト結果のスクリーンショットや[ビデオログ](https://help.autify.com/nocode-web/autify-university/foundation/test-results/video-logs-for-test-results.md)を確認し、ローディング画面が表示されていたり、ページが不完全な状態だったりした場合は、ページ読み込み完了前にJSステップが実行された可能性があります。

このケースの対応方法は[待機ステップ](https://help.autify.com/nocode-web/autify-university/foundation/creating-a-scenario/sleep-step.md)です。JSステップの前に待機ステップを挿入し、数秒待機してページの読み込みを待つようにしてください。

## エラーメッセージの原因と対応方法

次に、よくお問い合わせをいただくエラーメッセージについていくつか紹介します。

### Cannot read properties of null

オブジェクトのプロパティにアクセスをしようとして、そのオブジェクトが`null`の場合に発生します。例えばCSSセレクターで取得した要素に対してクリックをしたいとします。`document.querySelector()`に指定したCSSセレクターで取得できる要素がない場合、`null`が返ってきます。この返ってきた`null`に対して`click`を実行すると、以下のようにエラーが発生します。

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

指定したCSSセレクターで要素が取得できるか、[こちら](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-get-css-selectors.md#how-to-validate-css-selector-xpath)の記事を参考に再度確認してください。

### xxx is not defined

未定義の変数や関数を参照しようとした時に発生するエラーです。変数や関数が定義されていることを確認してください。

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

### Error: cannot find the element with selector/XPath

[Autify JavaScriptスニペット](https://javascript-snippets.autify.com/ja/)にあるいくつかのスニペットは、要素をCSSセレクター/XPathで取得してからクリックなどの操作を行っています。もし指定したCSSセレクター/XPathが誤っていたり、CSSセレクターにテスト実行のたびに変わる動的な値が含まれていたりすると、要素が取得できずエラーが発生しこのメッセージが表示されます。

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

指定したCSSセレクター/XPathで要素が取得できるか、[こちら](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-get-css-selectors.md#how-to-validate-css-selector-xpath)の記事を参考に再度確認してください。

## それでも問題が解決しない場合...

お気軽にカスタマーサポートチームにご連絡ください。問題を解決するお手伝いをいたします。

## 関連記事

* [CSSセレクター / XPathを取得する](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-get-css-selectors.md)
* [CSSセレクターを使いこなす](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-use-css-selectors-effectively.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/when-the-test-doesnt-work/common-errors/javascript-error.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.
