You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Selenium IDE中存储匹配CSS选择器的元素文本数组用于搜索测试

Solution for Selenium IDE: Store Multiple Element Texts as Array & Validate Search Results

Great question! Since you're restricted to only using Selenium IDE (no WebDriver or custom code), the execute script command is exactly the right tool to solve this problem. Here's a step-by-step breakdown of how to make it work:

Step 1: Use execute script to Fetch All Product Names into an Array

You can run a lightweight JavaScript snippet directly in Selenium IDE to grab all matching elements' text and store it as an array. Here's the script you'll need:

// Convert the NodeList to an array, then extract trimmed text from each element
return Array.from(document.querySelectorAll("ajax_block_product .product-container a.product-name"))
  .map(element => element.textContent.trim());

To set this up in your test case:

  1. Add a new execute script command
  2. Paste the above script into the Script field
  3. In the Value field, enter a variable name (like productNames) — this is where the array of product names will be saved

Step 2: Loop Through the Array to Validate Each Result

Now that you have all product names stored in productNames, use Selenium IDE's for each command to check every entry against your search keyword:

  1. Add a for each command
  2. In the Target field, enter your array variable: ${productNames}
  3. In the Value field, enter a variable to hold the current item (like currentProductName)

Inside the loop, add an assert command to verify the current product name includes your search keyword:

  • For the Target field, use this JavaScript snippet to check the match:
    storedVars['currentProductName'].includes(storedVars['searchKeyword'])
    
  • Set the Value field to true (this will fail the test if any product name doesn't contain the keyword)

(Note: Make sure you've already stored your search keyword in a variable like searchKeyword using a store command earlier in your test.)

Why store text Fails Here

The store text command in Selenium IDE is built to work with a single element — when you pass a selector that matches multiple elements, it only grabs the text from the first match. That's why execute script is necessary here: it lets you run custom logic to collect text from all matching elements in one go.

内容的提问来源于stack exchange,提问作者Victoria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 08:02:37