登录
首页 >  Golang >  Go问答

在chromedp中如何访问数据库来运行网络驱动程序?

来源:stackoverflow

时间:2024-03-03 16:54:24 126浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《在chromedp中如何访问数据库来运行网络驱动程序?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我想在银行页面上自动提交 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 期望所有参数都是 chromedp.tasks 类型,因此我无法在那里调用自定义函数,并且在从数据库获取 otp 时出现错误。我该如何解决这个问题?


正确答案


解决方案是将 otp 获取包装在 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)
}

以上就是《在chromedp中如何访问数据库来运行网络驱动程序?》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>