Skip to main content
The Get Text Contents command (GetTextContentsCommand) enables you to extract text content from DOM elements using CSS selectors. This command is essential for extracting visible text content from web pages.

Overview

The Get Text Contents command provides functionality for:
  • Extracting text content from DOM elements
  • Using CSS selectors to target elements
  • Filtering elements by various criteria
  • Chaining locators for nested searches
  • Returning arrays of text content from multiple elements

Parameters

ParameterTypeDescriptionRequired
elementsLocatorobjectElement locator configurationYes
sessionIdstringBrowser session identifierNo
keystringKey name for storing extracted textYes

Element Locator Configuration

The elementsLocator object configures how elements are targeted:
{
  "elementsLocator": {
    "selector": ".article-title",
    "timeout": 10000
  }
}

Locator Properties

PropertyTypeDescriptionRequired
selectorstringCSS selector for element targetingYes
timeoutnumberMaximum wait time in millisecondsNo
filterobjectFilter criteria (innerText, hasChildren, index)No
continueWithobjectChained locator for nested searchesNo

Usage Examples

Basic Text Extraction

{
  "command": "GetTextContents",
  "params": {
    "elementsLocator": {
      "selector": ".article-title",
      "timeout": 10000
    },
    "key": "article_titles"
  }
}

Text Extraction with Session

{
  "command": "GetTextContents",
  "params": {
    "sessionId": "#{UUID}",
    "elementsLocator": {
      "selector": ".article-title",
      "timeout": 10000
    },
    "key": "article_titles"
  }
}

Filtered Text Extraction

{
  "command": "GetTextContents",
  "params": {
    "elementsLocator": {
      "selector": ".article-title",
      "timeout": 10000,
      "filter": {
        "innerText": "Example",
        "hasChildren": false
      }
    },
    "key": "filtered_titles"
  }
}

Chained Locator

{
  "command": "GetTextContents",
  "params": {
    "elementsLocator": {
      "selector": ".article",
      "timeout": 10000,
      "continueWith": {
        "selector": ".title"
      }
    },
    "key": "nested_titles"
  }
}

Features

CSS Selectors

Target elements using standard CSS selectors:
  • Class selectors: .class-name
  • ID selectors: #element-id
  • Attribute selectors: [data-attribute="value"]
  • Descendant selectors: .parent .child
  • Pseudo-selectors: :first-child, :nth-child(n)

Filtering

Filter elements by various criteria:
  • innerText: Filter by text content
  • hasChildren: Filter by presence of child elements
  • index: Filter by element index

Chained Locators

Use continueWith for nested searches:
{
  "elementsLocator": {
    "selector": ".article",
    "continueWith": {
      "selector": ".title"
    }
  }
}

Returns Array

The command returns an array of text content from all matching elements:
{
  "article_titles": [
    "Title 1",
    "Title 2",
    "Title 3"
  ]
}

Variable Support

The Get Text Contents command supports variable interpolation in:
  • Selector: Use variables in CSS selectors
  • Session ID: Use #{UUID} for session tracking

Output Structure

The command stores extracted text under the specified key:

Single Element

{
  "article_title": "Example Title"
}

Multiple Elements

{
  "article_titles": [
    "Title 1",
    "Title 2",
    "Title 3"
  ]
}

Best Practices

  • Use Stable Selectors: Prefer class names and IDs over text-based selectors
  • Set Appropriate Timeouts: Configure timeouts for dynamic content loading
  • Wait for Elements: Ensure elements are loaded before extraction
  • Handle Empty Results: Account for cases where no elements match
  • Use Descriptive Keys: Use clear, descriptive key names for extracted text

Common Use Cases

  • Content Extraction: Extract text content from web pages
  • List Extraction: Extract text from lists and arrays
  • Title Extraction: Extract titles and headings
  • Description Extraction: Extract descriptions and summaries