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

使用Cypress脚本时遇「缺少分号」错误,.each语法报错求助

Cypress .each语法报错「缺少分号」的解决方案

你的代码存在语法错误,问题出在.each()的回调函数写法上。正确的写法是将箭头函数作为.each()的参数传入,而非把参数列表直接跟在.each()后面。

错误代码示例:

cy.get('.products').find('.product').each($el, index, $list) => { 
  const VegName = $el.find('h4.product-name').text()
  ...
}

修正后的代码:

cy.get('.products').find('.product').each(($el, index, $list) => { 
  const VegName = $el.find('h4.product-name').text()
  ...
})

关键说明:

  • 箭头函数整体需要包裹在.each()的括号内,遵循each(() => {})的结构
  • 回调的参数列表($el, index, $list)要放在箭头函数的箭头左侧,作为箭头函数的参数部分

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 22:42:45