> 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/tips-1/js-steps-tips/get-the-text-content-from-elements-using-js.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# JSステップで要素のテキストを取得する

## 要素のテキストを取得する

Autifyでは、JSステップで要素が持つテキストを取得し、後続のステップで使用することが可能です。

JSステップには以下2つの方法があります。

* [要素に対するJSステップ](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/js-steps-for-elements.md)で要素の取得はAutifyのAIに任せる方法。
* 通常の[JSステップ](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/javascript-steps.md)に[要素のテキストを取得する](https://javascript-snippets.autify.com/ja/snippets/values/get_text_content/)スニペットをコピー&ペーストして、自分で取得したCSSセレクターを設定する方法

### 要素に対するJSステップを使用する方法

[要素に対するJSステップ](https://help.autify.com/nocode-web/autify-university/advanced/js-steps/js-steps-for-elements.md)には、通常のJSステップと比べて以下の利点があります。

* AutifyのAIが要素を取得するのでCSSセレクター/XPathの取得を省ける。
* 要素取得の処理を書かなくて良いので比較的少ないコードで済む。
* ページのローディング完了や要素の出現を自動で待ってくれる。
* テスト結果のスクリーンショットで対象要素がハイライトされる。

#### ステップの作成方法

1. レコーディング中に対象の要素を右クリックし、**Autify Recorder > JSステップを追加する** を選択し、保存します。

   ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-1f01d6bc88c32268b3fad6aec4e6ea6ca3ca407d%2Fja-add-js-step.png?alt=media)
2. 引数に`element`という値が設定されたJSステップがシナリオに作成されます。この`element`にはAutifyのAIが取得した要素が格納されます。

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

すでに、要素が取得されている状態ですので、コード部分には要素の`textContent`を`return`する以下の一文を書くだけです。

```javescript
return element.textContent;
```

3. テストを実行すると要素のテキストの取得が確認できます。

   ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-93dae19a9b3378618e2dcb9d77f57c7a038b478a%2Fja-return-value-js-step-for-element.png?alt=media)

また通常のJSステップと違い、テスト結果のスクリーンショットで対象の要素がハイライトされるため、どの要素を対象としているのかがすぐに分かります。

![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-8ddfcbd33023908c894ac91bff5ac83a784a55e8%2Fja-screenshot-js-step-for-elemen.png?alt=media)

#### トラブルシューティング

要素の取得に失敗する場合は、以下の方法で解決する場合があります。

* [クイック要素更新](https://help.autify.com/nocode-web/autify-university/foundation/test-results/quick-element-update.md)で簡単に要素の更新を行えます
* [ロケータ](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/add-a-locator.md)の設定が有効です
  * ロケータを設定する場合は、CSSセレクター/XPathの取得が必要ですが、他の3つの利点は引き続き得ることができます

### 通常のJSステップとスニペットを使用する方法

基本的には要素に対するJSステップを使用する方法をお勧めしますが、対象のページで独自の右クリックメニューを実装している場合、要素に対するJSステップを追加することができません。その際は、通常のJSステップを使用した方法をお使いください。

#### ステップの作成方法

1. JSステップをシナリオに挿入し、[要素のテキストを取得する](https://javascript-snippets.autify.com/ja/snippets/values/get_text_content/)スニペットをコピーしてJSステップに貼り付けます。
2. [CSSセレクター / XPathを取得する](https://help.autify.com/nocode-web/autify-university/advanced/locator-feature/how-to-get-css-selectors.md)を参考に対象要素のCSSセレクターを取得し、`var selector = "<TODO: REPLACE SELECTOR>";`の`<TODO: REPLACE SELECTOR>`と置き換えます。

```javascript
// 例
var selector = "#doc_header > div.landing_top > div > div.landing_top_inner.center > h1";
```

3. テストを実行すると要素のテキストの取得が確認できました。

   ![](https://50135409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fof7cb8AGXhWvWThq3R0w%2Fuploads%2Fgit-blob-05a02b230e65986c183ebb8d3a20941fd1be79d2%2Fja-return-value-normal-js-step.png?alt=media)

## その他

* `textContent`で希望する要素のテキストが取得できない場合は、`innerText`を使用することで改善する場合があります。


---

# 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/tips-1/js-steps-tips/get-the-text-content-from-elements-using-js.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.
