node.js - 使用 Nodejs 在 selenium 中获取 Promise Pending

标签 node.js selenium selenium-webdriver

我正在尝试使用 selenium 和 Nodejs 创建银行登录自动化,但陷入了 Promise{pending}。

const {Builder, By, Key, until} = require('selenium-webdriver');

async function main() {
let driver = await new Builder()
    .forBrowser('chrome')
    .build();

await driver.get('login-url')
const name = await driver.findElement(By.name('AuthenticationFG.USER_PRINCIPAL'))
const pass = await driver.findElement(By.name('AuthenticationFG.ACCESS_CODE'))

await name.sendKeys(username)
await pass.sendKeys(password)

await driver.findElement(By.id("VALIDATE_CREDENTIALS1")).click()
await driver.wait(until.elementIsVisible(driver.findElement(By.id('LoginName'))), 1000)
await console.log(driver.findElement(By.id('LoginName'))) <-- getting issue here

}
main()

我试图在登录后从页面抓取后打印用户名,但它继续给出 Promise {pending}。

如有任何帮助,我们将不胜感激。

最佳答案

正如 AbhinavD 指出的用途:

main().then((result) => {
// do stuff here
})

你的主要函数是async,它返回一个Promise。 Promise 是一种可以解决、拒绝或待处理的数据结构。当 promise 得到解决时,您可以对其调用 .then() 回调。这是一个可以说明这一点的示例:

 async function test () {

  let randomvalue = 5;

  return randomvalue;
}

let returnValue = test()

// this logs the promise object
console.log(returnValue);

// this logs the actual value returned in the async function
returnValue.then((value) => {
	console.log(value)
});

async 函数本质上是用返回值解析一个 Promise。

对于任何想要经常使用 JS 的人来说,Promise 本身就是一个必须学习的主题。所以here更详细的来源:

关于node.js - 使用 Nodejs 在 selenium 中获取 Promise Pending,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50718241/

相关文章:

java - @FindBy 查找 webelement 返回 null

python - 使用 selenium python 网络驱动程序从 Angular 单击表格中的所有行

python - 从 Python 控制台运行 Selenium webdriver

javascript - 如何使用拼贴组件在 react 中动态加载图像

javascript - preg_match 到 Javascript 函数

selenium - 如何验收测试受验证码保护的 Web 应用程序功能?

java - 通过 CLI : Could not find or load main class org. testng.TestNG 执行 testng.xml 时出错

javascript - MongoDB .find 在 shell 中有效,但在 js 中无效

javascript - 来自 Angular 形式的 $http.post 不发送任何数据

javascript - WebElement 位置与 window().getSize() 的预期值不匹配