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

Protractor元素调用链报错求助:element.all使用失败

Fixing the element(...).element.all is not a function Error in Protractor

Hey there! Let's break down what's going wrong with your code and get it working again.

The Root Cause

The error pops up because element() returns a single ElementFinder object, and it doesn't have an element.all() method. The element.all() syntax is for locating multiple elements directly from the browser context, not from a parent ElementFinder. To fetch multiple child elements under a parent, you should use .all() instead of .element.all().

Corrected Code Options

Option 1: Fix the Chained Call

Swap .element.all() with .all() in your existing code chain:

expect(element(by.css('.textLayer')).all(by.tagName('div')).get(6).getText()).toContain(expectedString);
  • element(by.css('.textLayer')) locates the parent div
  • .all(by.tagName('div')) grabs all child div elements under that parent
  • .get(6) selects the 7th child (Protractor uses 0-based indexing)
  • .getText() retrieves the text content for your assertion

Option 2: Use a More Efficient CSS Selector

You can simplify this even further by targeting the 7th child div directly with a CSS selector (note: CSS :nth-child uses 1-based indexing):

expect(element(by.css('.textLayer div:nth-child(7)')).getText()).toContain(expectedString);

This skips fetching all child divs and directly targets the element you need, making your test more performant.

Additional Tips

  • Ensure the .textLayer element is present in the DOM before your test runs. Use Protractor's ExpectedConditions to wait for it to be visible:
    const EC = protractor.ExpectedConditions;
    browser.wait(EC.visibilityOf(element(by.css('.textLayer'))), 5000);
    
  • Double-check that there are at least 7 child divs under .textLayer—if not, .get(6) will throw an out-of-bounds index error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:15:09