chromedp.ActionFunc中赋值的变量无法通过SendKeys输入

Variables assigned in chromedp.ActionFunc cannot be inputted by SendKeys

我想在 chromedp.ActionFunc 中经过一些处理后,将字符串分配给一个变量。 参考下面的例子。
但是,chromedp.ActionFunc之后的chromedp.SendKeys无法将变量输入到表单中。
我用 chromedp.CaptureScreenshot.

确认了以上内容
package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "log"
    "time"

    "github.com/chromedp/chromedp"
)

func main() {
    ctx, cancel := chromedp.NewContext(
        context.Background(),
        chromedp.WithLogf(log.Printf),
    )
    defer cancel()

    ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
    defer cancel()

    var value string
    var buf0 []byte
    var buf1 []byte
    err := chromedp.Run(ctx, chromedp.Tasks{
        chromedp.Navigate(`https://whosebug.com/users/login`),
        chromedp.WaitVisible(`//*[@id="submit-button"]`),
        chromedp.CaptureScreenshot(&buf0),
        chromedp.ActionFunc(func(c context.Context) error {
            value = "apple"
            return nil
        }),
        chromedp.SendKeys(`//*[@id="email"]`, value),
        chromedp.CaptureScreenshot(&buf1),
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Print(value)
    ioutil.WriteFile("./capture0.png", buf0, 0644)
    ioutil.WriteFile("./capture1.png", buf1, 0644)
}

如果chromedp.SendKeys包含在chromedp.ActionFunc中,则成功。
这个方法没有问题。
但是我想知道为什么上面的方法不起作用。
如果您对chromedp有所了解,请告诉我。

        chromedp.ActionFunc(func(c context.Context) error {
            value = "apple"
            chromedp.SendKeys(`//*[@id="email"]`, value).Do(c)
            return nil
        }),

第一种方法目前不起作用

chromedp.SendKeys(`//*[@id="email"]`, value)

被调用时,变量 value 的值为空,因此它有效地创建了带有空字符串的 SendKeys 操作。如果在调用 chromedp.SendKeys 之前初始化变量,则行为将如预期的那样,请参阅固定代码:

package main

import (
    "context"
    "fmt"
    "io/ioutil"
    "log"
    "time"

    "github.com/chromedp/chromedp"
)

func main() {
    ctx, cancel := chromedp.NewContext(
        context.Background(),
        chromedp.WithLogf(log.Printf),
    )
    defer cancel()

    ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
    defer cancel()

    var value string = "apple"
    var buf0 []byte
    var buf1 []byte
    err := chromedp.Run(ctx, chromedp.Tasks{
        chromedp.Navigate(`https://whosebug.com/users/login`),
        chromedp.WaitVisible(`//*[@id="submit-button"]`),
        chromedp.CaptureScreenshot(&buf0),
        chromedp.SendKeys(`//*[@id="email"]`, value),
        chromedp.CaptureScreenshot(&buf1),
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Print(value)
    ioutil.WriteFile("./capture0.png", buf0, 0644)
    ioutil.WriteFile("./capture1.png", buf1, 0644)
}

编辑: 这也将按您的预期工作。

    err := chromedp.Run(ctx, chromedp.Tasks{
        chromedp.Navigate(`https://whosebug.com/users/login`),
        chromedp.WaitVisible(`//*[@id="submit-button"]`),
        chromedp.CaptureScreenshot(&buf0),
        chromedp.ActionFunc(func(c context.Context) error {
            value = "apple"
            return nil
        }),
    })
    if err != nil {
        log.Fatal(err)
    }

    err = chromedp.Run(ctx, chromedp.Tasks{
        chromedp.SendKeys(`//*[@id="email"]`, value),
        chromedp.CaptureScreenshot(&buf1),
    })