请考虑以下代码:
//CL.DCBS_List_AllElements = $$('#directClassBased > option')
this.shouldMatchDCBSList = async function () {
var DCBSItems = await CL.DCBS_List_AllElements;
console.log('Test Text = '+await CL.DCBS_List_AllElements.get(3).getText());
console.log('Test Text 2 = '+await DCBSItems.get(4).getText());
当我执行这段代码时,第一个 console.log (console.log('Test Text = '+await CL.DCBS_List_AllElements.get(3).getText());) 按预期返回文本。但对于下一个,当我将它用作变量(“await DCBSItems.get(4).getText()”)时,它给了我以下错误:
Failed: DCBSItems.get is not a function
这有点奇怪,因为当我使用参数/变量传递元素时,我可以获得之前返回的文本。我花了很多时间来修复它,但现在不起作用。我们该如何解决它?
最佳答案
如果您等待 ElementArrayFinder,您将获得一个 ElementFinder 数组。请参阅https://github.com/angular/protractor/blob/master/lib/element.ts#L535
当调用ElementArrayFinder.get(index)
时,Protractor将获取ElementArrayFinder对象中的index
。如果您等待 ElementArrayFinder,您将获得一个数组。因此,在您使用 DCBSItems
的情况下,您应该调用 await DCBSItems[4].getText()
,而不是调用 .get
。
关于javascript - 使用局部变量调用时,protractor .get 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192044/