Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Chrome headless with chromedp?

I'm using chromedp, which has features to focus on elements, fill in text, etc. Chrome 59 has cross-platform headless support. It allows running Chrome in a headless/server environment. To use via the DevTools remote debugging protocol, start a normal Chrome binary with the --headless command line flag (Linux-only for now):

$ google-chrome --headless --disable-gpu --remote-debugging-port=9222 https://www.google.fr

How can I tell chromedp to send the --headless flag, along with other flags?

like image 206
LeMoussel Avatar asked Oct 29 '25 17:10

LeMoussel


2 Answers

In the latest version of chromedp, by default the headless mode is true, if you want to change then refer the below snippet

opts := append(chromedp.DefaultExecAllocatorOptions[:],
    chromedp.Flag("headless", false),
    chromedp.Flag("disable-gpu", false),
    chromedp.Flag("enable-automation", false),
    chromedp.Flag("disable-extensions", false),
)

allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()

// create context
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()

if err := chromedp.Run(ctx,
    chromedp.Navigate(`https://www.google.com/`),
); err != nil {
    log.Fatal(err)
}
like image 109
Nagendran Avatar answered Oct 31 '25 10:10

Nagendran


Find It. I do

c, err := cdp.New(ctxt, cdp.WithRunnerOptions(
   runner.Flag("headless", true),
   runner.Flag("disable-gpu", true)))
if err != nil {
    log.Fatal(err)
}
like image 31
LeMoussel Avatar answered Oct 31 '25 09:10

LeMoussel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!