Javascript 有两个相同的接收值​;来自承诺:在一种情况下它工作,在另一种情况下它给出一个TypeError:x不是一个函数

Javascript 有两个相同的接收值​;来自承诺:在一种情况下它工作,在另一种情况下它给出一个TypeError:x不是一个函数,javascript,node.js,selenium,selenium-webdriver,Javascript,Node.js,Selenium,Selenium Webdriver,我在自动测试中使用元素搜索,并从列表中获取名称。我的代码工作正常,一切正常。但在自动测试中,我多次使用此代码。因此,我决定将它放入一个函数中,并在需要时调用它。 守则运作: await driver.wait(until.elementLocated(By.className("item")), 20000); let findItems1 = await driver.findElements(By.className("item")); let items1 = findItem

我在自动测试中使用元素搜索,并从列表中获取名称。我的代码工作正常,一切正常。但在自动测试中,我多次使用此代码。因此,我决定将它放入一个函数中,并在需要时调用它。 守则运作:

  await driver.wait(until.elementLocated(By.className("item")), 20000);
  let findItems1 = await driver.findElements(By.className("item"));
  let items1 = findItems1.map(async elem => await elem.getText());
  await Promise.all(items1);

  let currentItem1 = findItems1[findItems1.length - 1];
  await currentItem1.click();

  currentName = await currentItem1.getText();  // This string operates
  await Promise.all(currentName)
  console.log(currentName)
我从承诺所在的函数中推断变量的值。我可以点击这个项目。但是,当我想从承诺中获取文本值时,字符串“currentName=await currentItem1.getText()”会抛出一个错误。虽然在我的第一段代码中,这行代码是有效的。我不明白是什么原因

代码不工作:

async function findCurrentItem(){
    await driver.wait(until.elementLocated(By.className("item")), 20000);
    let findItems = await driver.findElements(By.className("item"));  
    let items = findItems.map(async elem => await elem.getText());
    await Promise.all(items);
    let currentItem = findItems[findItems.length - 1];
    return currentItem;        
  }
 let current = findCurrentItem();
  await currentItem1.click();
 console.log(current, 1)    // console displays promise
 let currentName = await current.getText(); // This string doesn't operate
 await Promise.all(currentName)
 console.log(currentName, 2)   // console displays error
错误:

TypeError: currentItem.getText is not a function

我能做什么?

您创建了findCurrentItem异步函数,但在使用它时不要等待结果。 更改为
let current=wait findCurrentItem()