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

TestCafe自动化测试中CSS选择器index参数类型错误的解决方案咨询

TestCafe动态CSS定位器报错的解决办法

问题根源

你的代码存在两个问题触发报错:

  1. 函数参数重复定义了index,会导致变量值被覆盖
  2. 使用模板字符串${index}将数字转为字符串传给nth(),而TestCafe的nth()方法要求传入数字类型参数

解决步骤

  • 移除函数参数中重复的index,修正后的参数列表为(index, name, date, subTitle, desc)
  • 直接将index数字传入nth(),不要用模板字符串转换类型

修正后的代码

this.infoQCLog = (index, name, date, subTitle, desc) => 
  Selector('*[data-element-name = "content-checklog"]')
    .nth(index)
    .child('div').withText(`${name}`)
    .nextSibling('div').withText(`${date}`)
    .nextSibling('div').withText(`${subTitle}`)
    .nextSibling('div').withText(`${desc}`)

补充说明

如果调用infoQCLog时传入的index本身是字符串类型,需要先转为数字,比如用parseInt(index, 10)或者Number(index)转换后再传入。

内容的提问来源于stack exchange,提问作者Hiếu Đinh Trần

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 13:15:56