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

Protractor中if/else逻辑失效求助:报错‘eachOperator.toEqual is not a function’

解决Protractor中eachOperator.toEqual is not a function错误

嘿,我来帮你搞定这个问题!你遇到的eachOperator.toEqual is not a function错误,核心原因是把Jasmine的断言方法用错了地方。

错误原因解析

eachOperator是Protractor返回的ElementFinder对象,而toEqual()是Jasmine框架里用来做测试断言的方法——它只能在expect()语句里使用,根本不能直接调用在元素对象上。你想要判断选项文本是不是'-',得先获取元素的实际文本内容才行。

修正后的代码

下面是调整后的正确写法,我们用getText()获取选项文本,再判断是否符合条件后执行点击:

var selectDropdownbyNum = function (element, optionNum) {
  var opt = element.all(by.tagName('option'));
  opt.each(function (eachOperator) {
    // 先获取选项的文本内容
    eachOperator.getText().then(function (operatorText) {
      // 判断文本是否为'-',是则点击
      if (operatorText === '-') {
        eachOperator.click();
      }
    });
  });
};

更高效的写法(可选)

如果你的需求是找到第一个文本为'-'的选项并点击,其实不用遍历所有选项,直接定位目标元素效率更高:

var selectDropdownbyNum = function (element) {
  // 方式1:通过value属性定位(如果选项有value="-"的话)
  element.element(by.css('option[value="-"]')).click();
  
  // 方式2:通过文本内容定位(更通用)
  // element.element(by.cssContainingText('option', '-')).click();
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:53:58