Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

screenshot from a wrong page #205

Closed
bhpike65 opened this issue Jun 25, 2018 · 0 comments
Closed

screenshot from a wrong page #205

bhpike65 opened this issue Jun 25, 2018 · 0 comments

Comments

@bhpike65
Copy link

bhpike65 commented Jun 25, 2018

  1. start to run a headless chrome in background
    nohup /usr/bin/google-chrome --headless --disable-gpu --remote-debugging-port=9222 --no-sandbox --no-default-browser-check --no-first-run &

  2. start different instances and take screenshot in the same time
    but they return a same page. it seems that the instance and the page not mapping correctly, and instance cannot be Shutdown() how it closes the page?

./screenshot https://www.google.com google.png
./screenshot https://www.bing.com bing.png

package main

import (
        "context"
        "io/ioutil"
        "log"
        "time"
        "os"
        "github.com/chromedp/cdproto/cdp"
        "github.com/chromedp/cdproto/page"
        "github.com/chromedp/chromedp"
        "github.com/chromedp/cdproto/emulation"
)

func main() {
        if len(os.Args) != 3 {
                panic("argc")
        }
        site := os.Args[1]
        filename := os.Args[2]

        var err error

        // create context
        ctxt, cancel := context.WithCancel(context.Background())
        defer cancel()

        // create chrome instance
        c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
        if err != nil {
                log.Fatal(err)
        }

        metrics := emulation.SetDeviceMetricsOverride(1920, 1080, 0, false)
        if err = c.Run(ctxt, metrics); err != nil {
                log.Println(err)
                return
        }

        // run task list
        var buf []byte

        if err = c.Run(ctxt, screenshotTask(site, &buf)); err != nil {
                log.Println(err)
                return
        }
/*
        if err = c.Shutdown(ctxt); err != nil {
                log.Println(err)
        }
*/
        // write to disk
        err = ioutil.WriteFile(filename, buf, 0644)
        if err != nil {
                log.Println(err)
                return
        }
}

func screenshotTask(url string, picbuf *[]byte) chromedp.Action {
        return chromedp.Tasks{
                /*
                chromedp.ActionFunc(func(ctx context.Context, h cdp.Executor) error {
                        _, err := network.SetCookie("customKey", "meaningless").WithURL(url).Do(ctx, h)
                        return err
                }),
                */
                chromedp.Navigate(url),
                chromedp.Sleep(5000 * time.Millisecond),
                chromedp.ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
                        buf, err := page.CaptureScreenshot().Do(ctxt, h)
                        if err != nil {
                                return err
                        }
                        *picbuf = buf
                        return nil
                }),
        }
}

@kenshaw kenshaw closed this as completed Jul 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants