dsfdsf23423 2018-01-29 16:27
浏览 1065
已采纳

Chromedp不在ActionFunc的循环内执行操作

I have a slice containing a list of terms, and I want to search for each term in a certain search engine page, so I am doing this:

func risk(slice []string) chromedp.Tasks {

    return chromedp.Tasks{
        chromedp.Navigate("https://testpage"),
        chromedp.WaitVisible("#query_box", chromedp.ByID),
        chromedp.ActionFunc(func(context.Context, cdp.Executor) error {
            for _, element := range slice[2:] {
                fmt.Println(element)
                chromedp.SendKeys("#query_box", element, chromedp.ByID)
                chromedp.Click("#searchButton", chromedp.ByID),
            }
            return nil
        }),
    }
} 

When calling this inside main as

err = c.Run(ctxt, risk(items))
    if err != nil {
        log.Fatal(err)
    }

Everything works until the ActionFunc. Whatever action I can add before the function (take screenshot, etc...) work without issues.

However, the actions inside ActionFunc don't get performed.

Is the reason the return nil? I wanted to return the set of tasks like I am doing outside the loop, but I couldn't find about how to do that inside a loop in the ActionFunc, since the return would always be the latest item rather than the full set... Returning nil was the only way to get the function to at least get started.

What is the correct way to perform this sort of loop operations inside a set of chromedp.Tasks?

  • 写回答

1条回答 默认 最新

  • dsd119120 2018-01-30 17:17
    关注

    chromedp.ActionFunc is used to build a custom action. The function you give it will be executed during the Run phase.

    This means that your function needs to actually run the actions you are using inside. This is done by calling the .Do method on the action and passing it a context.Context and cdp.Executor.

    As for errors, the function should return any error encountered while running. When calling .Do on your embedded actions, check the error and return it if non-nil.

    Your code should look like:

    func risk(slice []string) chromedp.Tasks {
    
        return chromedp.Tasks{
            // ... other actions ...
            chromedp.ActionFunc(func(c context.Context, e cdp.Executor) error {
                for _, element := range slice[2:] {
                    fmt.Println(element)
                    err := chromedp.SendKeys("#query_box", element, chromedp.ByID).Do(c, e)
                    if err != nil {
                      return err
                    }
                    err = chromedp.Click("#searchButton", chromedp.ByID).Do(c, e)
                    if err != nil {
                      return err
                    }
                }
                return nil
            }),
        }
    }
    

    disclaimer: I haven't tested this code, so there may be issues, but this should give you the general idea of defining vs executing an action, and properly returning errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效