0

我想在银行页面上自动提交 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 时出错。我该如何解决这个问题?

4

1 回答 1

1

解决方案是将 otp fetch 包装在Action.Do调用中,然后返回调用结果chromdp.SendKeys以设置 HTML 输入值。

必须以这种方式工作,因为在获取页面之前一次性密码不存在,因此,必须在操作资源时读取它。

像这样

package main

import "context"

type OTPAction struct {
    // DB ....
}

func (a OTPAction) Do(ctx context.Context) error {
    // fetch OTP here
    otp := "otp test"
    return chromedp.SendKeys(`//input[@id="user-message"]`, otp).Do(ctx)
}
于 2021-07-27T12:21:56.023 回答