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.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)