Javascript Selenium在JS中使用async/await,查找并单击元素

Javascript Selenium在JS中使用async/await,查找并单击元素,javascript,selenium-webdriver,ecmascript-2017,Javascript,Selenium Webdriver,Ecmascript 2017,我正在尝试使用SeleniumWebDriver和Mocha对ES7进行重构,并使用异步/等待功能。我有以下代码: await loginPage.loginAsAdmin() /* THIS DOES NOT WORK */ //await layout.Elements.routePageButton().click() /* THIS DOES WORK */ let a = await layout.Elements.routePageButton() await a.click()

我正在尝试使用SeleniumWebDriver和Mocha对ES7进行重构,并使用异步/等待功能。我有以下代码:

await loginPage.loginAsAdmin()

/* THIS DOES NOT WORK */
//await layout.Elements.routePageButton().click()

/* THIS DOES WORK */
let a = await layout.Elements.routePageButton()
await a.click()
我不明白为什么这一点不起作用-我得到:

TypeError: layout.Elements.routePageButton(...).click is not a function
单击之前的函数方法返回webElement,如您所见:

布局:

routePageButton:  async () => await findVisibleElement('#route_info a')
const findVisibleElement = utils.Methods.Element.findVisible
方法:

findVisible: async (cssSelector) => {
  let elm = await driver().findElement(by.css(cssSelector))
  return elm
}

这里的问题是错误地认为
await
是ES2017中的一个语言关键字,它允许您阻止调用
async
函数的执行,直到被调用函数返回的
Promise
得到解决

routePageButton()
返回一个
Promise
,这就是上面第二种语法工作的原因,因为执行被阻止,直到
Promise
解析为
WebElement
对象

但是,在第一个示例中使用的语法中,它试图等待的函数(
click()
)从未被调用,因为
Promise
没有
click()
函数。注意,在第二种语法中有两个
wait
s,但在第一种语法中只有一个

要在一行中完成您试图完成的操作,您必须执行以下操作:

await (await layout.Elements.routePageButton()).click()

async/await
是ES2017的一部分,而不是ES2016(ES7)。感谢您的澄清,很抱歉我弄错了,我很快就会编辑。