如何在使用 chromedp 运行 webdriver 时访问数据库?

问题描述

我想在银行页面上自动提交 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.Click(`#confirmButton`))
    if err != nil {
        log.Fatal(err)
    }

问题是 chromedp.Run 期望所有 args 都是 chromedp.Tasks 类型,所以我无法在那里调用自定义函数,并且在从 db 获取 OTP 时出错。我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)