Protractor元素调用链报错求助:element.all使用失败
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
.textLayerelement is present in the DOM before your test runs. Use Protractor'sExpectedConditionsto 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

