【发布时间】:2021-10-02 15:31:10
【问题描述】:
我想在银行页面上自动提交 OTP。只有在 webdriver 单击银行页面上的确认后,我才会在我的数据库中获取 OTP。确认后,我需要从数据库中获取 OTP,然后自动提交 OTP。
ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithDebugf(log.Printf))
defer cancel()
// run chromedp tasks
err := chromedp.Run(ctx,
chromedp.Navigate(bankUrl),
chromedp.WaitVisible(`#username`),
chromedp.SendKeys(`#username`, `usernameXXX`),
chromedp.WaitVisible(`#label2`, ),
chromedp.SendKeys(`#label2`, `passwordxxx` ),
chromedp.Click(`//input[@title="Login"]`),
chromedp.WaitVisible(`#Go`),
chromedp.Click(`#Go`),
chromedp.WaitVisible(`#confirmButton`),
chromedp.Click(`#confirmButton`),
chromedp.WaitVisible(`//input[@type="password"]`),
// perform fetch OTP below, this raise error
otp := fetchOTPFromDb()
chromedp.SendKeys(`//input[@type="password"]`, otp),
chromedp.WaitVisible(`#confirmButton`),
chromedp.Click(`#confirmButton`))
if err != nil {
log.Fatal(err)
}
问题是 chromedp.Run 期望所有 args 都是 chromedp.Tasks 类型,所以我不能在那里调用自定义函数,并且在从 db 获取 OTP 时出错。我该如何解决这个问题?
【问题讨论】:
-
一次性密码的一次性密码?我猜,您想获取该 OTP,然后在提交之前将其注入网页?
-
是的@mh-cbon。问题是我仅在完成自动化步骤的一半后才收到 OTP。所以我不得不偏离 chromedp.Tasks 并调用一些内部函数来获取 OTP。
-
我猜不可能在调用 chromedp.Run 之前进行 fetch 操作,因为数据还不存在?
-
如果您尝试play.golang.org/p/_oNQfWHwTxz 会怎样?
-
工作,非常感谢@mh-cbon。
标签: go automation browser-automation headless-browser chromedp