How to use Chrome headless with chromedp?

huangapple go评论119阅读模式
英文:

How to use Chrome headless with chromedp?

问题

我正在使用 chromedp,它具有聚焦元素、填写文本等功能。Chrome 59 支持跨平台无头模式,可以在无头/服务器环境中运行 Chrome。要通过 DevTools 远程调试协议使用,需要在正常的 Chrome 二进制文件上使用 --headless 命令行标志(目前仅适用于 Linux):

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

我该如何告诉 chromedp 发送 --headless 标志以及其他标志?

英文:

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?

答案1

得分: 11

在最新版本的chromedp中,默认情况下是启用无头模式的。如果你想要改变这个设置,请参考下面的代码片段:

  1. opts := append(chromedp.DefaultExecAllocatorOptions[:],
  2. chromedp.Flag("headless", false),
  3. chromedp.Flag("disable-gpu", false),
  4. chromedp.Flag("enable-automation", false),
  5. chromedp.Flag("disable-extensions", false),
  6. )
  7. allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
  8. defer cancel()
  9. // 创建上下文
  10. ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
  11. defer cancel()
  12. if err := chromedp.Run(ctx,
  13. chromedp.Navigate(`https://www.google.com/`),
  14. ); err != nil {
  15. log.Fatal(err)
  16. }

以上代码片段可以用于修改chromedp的默认设置,以在启动时禁用无头模式。

英文:

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

  1. opts := append(chromedp.DefaultExecAllocatorOptions[:],
  2. chromedp.Flag("headless", false),
  3. chromedp.Flag("disable-gpu", false),
  4. chromedp.Flag("enable-automation", false),
  5. chromedp.Flag("disable-extensions", false),
  6. )
  7. allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
  8. defer cancel()
  9. // create context
  10. ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
  11. defer cancel()
  12. if err := chromedp.Run(ctx,
  13. chromedp.Navigate(`https://www.google.com/`),
  14. ); err != nil {
  15. log.Fatal(err)
  16. }

答案2

得分: 8

找到它。我做

  1. c, err := cdp.New(ctxt, cdp.WithRunnerOptions(
  2. runner.Flag("headless", true),
  3. runner.Flag("disable-gpu", true)))
  4. if err != nil {
  5. log.Fatal(err)
  6. }
英文:

Find It. I do

  1. c, err := cdp.New(ctxt, cdp.WithRunnerOptions(
  2. runner.Flag("headless", true),
  3. runner.Flag("disable-gpu", true)))
  4. if err != nil {
  5. log.Fatal(err)
  6. }

huangapple
  • 本文由 发表于 2017年5月19日 18:00:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/44067030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定