Skip to main content

conditional-logic Scenario 7 — Search Fallback with Alternate Keyword

Written by RTILA X Engineering Team

Advanced ~10 min
Prerequisites: conditional-logic/3 conditional-logic/4 conditional-logic/5

What You'll Learn

Implement search fallback with alternate inputs

Real-World Use Case

Retrying with different search terms

LIVE DEMO

Search Fallback

Detect zero results and retry with a fallback keyword before extracting.

EXPECTED OUTPUT

Fallback Results

id title
1 Smartphone X Pro 256GB
2 Smartphone X 128GB
3 Smartphone X Mini
4 Smartphone Y 256GB
5 Smartphone Y 512GB
RTILA X PROJECT JSON

RTILA X Automation Project

search-fallback-alternate-keyword.json
{
  "name": "Search_Fallback_Alternate_Keyword",
  "settings": {
    "urls": [
      "https://learn.rtila.com/scenarios/conditional-logic/7"
    ]
  },
  "datasets": {
    "fallback_results": {
      "item_selector": "css=.search-result-item",
      "properties": [
        {
          "name": "title",
          "type": "text",
          "selector": "css=.result-title"
        }
      ]
    }
  },
  "commands": [
    {
      "command": "fill",
      "params": {
        "selector": "css=#fallback-search",
        "value": "phones"
      }
    },
    {
      "command": "wait_for_selector",
      "params": {
        "selector": "css=#fallback-search-btn"
      }
    },
    {
      "command": "click",
      "params": {
        "selector": "css=#fallback-search-btn"
      }
    },
    {
      "command": "wait",
      "params": {
        "timeout": 1000
      }
    },
    {
      "command": "execute_script",
      "params": {
        "script": "return document.querySelector('.result-count').textContent.includes('0 results') ? 'true' : 'false';",
        "output_variable": "no_results"
      }
    },
    {
      "command": "if",
      "params": {
        "condition": {
          "type": "variable_contains",
          "variable": "no_results",
          "value": "true"
        },
        "then": [
          {
            "command": "fill",
            "params": {
              "selector": "css=#fallback-search",
              "value": "smartphones"
            }
          },
          {
            "command": "wait_for_selector",
            "params": {
              "selector": "css=#fallback-search-btn"
            }
          },
          {
            "command": "click",
            "params": {
              "selector": "css=#fallback-search-btn"
            }
          },
          {
            "command": "wait",
            "params": {
              "timeout": 1000
            }
          }
        ],
        "else": []
      }
    },
    {
      "command": "wait_for_selector",
      "params": {
        "selector": "css=.search-result-item"
      }
    },
    {
      "command": "extract_data",
      "params": {
        "dataset": "fallback_results"
      }
    }
  ]
}

Search fallback patterns leverage a script condition to read the result count. If zero results are detected, the automation replaces the query with a successful fallback keyword, performs the new search, and extracts the returned items.

Did you complete this scenario?

Ready to run this automation?

Copy the project JSON above, open RTILA X, create a new project, and paste it.

This scenario covers 2 RTILA X commands and is referenced in 2 learning articles.

Continue Learning

Was this helpful?