0

This is my code:

package main

import (
        "context"
        "log"
        "fmt"
        "github.com/chromedp/chromedp"
)

func main() {
        queries := [3]string{"Object.keys(window);", "window.CSS", "window.Array"}

        // create context
        ctx, cancel := chromedp.NewContext(context.Background())
        defer cancel()

        // run task list
        var res []byte
        err := chromedp.Run(ctx,
                chromedp.Navigate(`https://www.google.com/`),
                chromedp.ActionFunc(func(ctx context.Context) error {
                for _, query := range queries {

                        err2 := chromedp.Evaluate(query, &res)
                        if err2 != nil {
                                fmt.Printf("error in ActionFunc: %s\n", err2)
                        }

                        fmt.Printf("Query %s outputs: %v\n", query, res)
                }
        return nil
        }),
        )
        if err != nil {
                log.Fatal(err)
        }

}

What i am trying to do is to navigate to url to Evaluate and get values for a big list of queries (I reduced the array to 3 queries for the example).

and then it should just outputs the values from those queries.

But what i get is these errors for every iteration:

error in ActionFunc: %!s(chromedp.ActionFunc=0x7f25a0)
Query Object.keys(window); outputs: []
error in ActionFunc: %!s(chromedp.ActionFunc=0x7f25a0)
Query window.CSS outputs: []
error in ActionFunc: %!s(chromedp.ActionFunc=0x7f25a0)
Query window.Array outputs: []
Raywando
  • 129
  • 5

1 Answers1

2

chromedp.Evaluate does not return error. It returns EvaluateAction. It has Do func which accepts context. So you can try this;

queries := [3]string{"Object.keys(window);", "window.CSS", "window.Array"}

    // create context
    ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()

    // run task list
    var res []byte
    err := chromedp.Run(ctx,
        chromedp.Navigate(`https://www.google.com/`),
        chromedp.WaitReady("body"),
        //chromedp.Evaluate("Object.keys(window)", &res),
        chromedp.ActionFunc(func(ctx context.Context) error {
            for _, query := range queries {

                chromedp.Evaluate(query, &res).Do(ctx)

                fmt.Printf("Query %s outputs: %+v\n", query, string(res))

            }
            return nil
        }),
    )
    if err != nil {
        log.Fatal(err)
    }
    
sigkilled
  • 227
  • 1
  • 7
  • Thanks! I have another question that might be a bit off-topic, but is there a way to check if there is an alert that popped up using chromedp? Or do I need a (headful) browser for that? – Raywando Apr 15 '21 at 22:08
  • 1
    You are welcome. I don’t think you need a headful browser instance for that. You can register a listener for browser events such as javascript dialog. There is a code snippet, please take a look at this: https://github.com/chromedp/chromedp/issues/92#issuecomment-579847003 – sigkilled Apr 16 '21 at 00:19