我正在尝试使用 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/