Skip to content

Added an example for non-headless mode which displays browser window #23

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ The following examples are currently available:
<!-- the following section is updated by running `go run gen.go` -->
<!-- START EXAMPLES -->
| Example | Description |
|---------------------------|----------------------------------------------------------------------------|
| ------------------------- | -------------------------------------------------------------------------- |
| [click](/click) | use a selector to click on an element |
| [cookie](/cookie) | set a HTTP cookie on requests |
| [emulate](/emulate) | emulate a specific device |
| [eval](/eval) | evaluate javascript and retrieve the result |
| [head](/head) | use headless flag to display browser window |
| [headers](/headers) | set a HTTP header on requests |
| [keys](/keys) | send key events to an element |
| [logic](/logic) | more complex logic beyond simple actions |
Expand Down
35 changes: 35 additions & 0 deletions head/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Command head (as opposed to headless) is a chromedp example demonstrating
// how to use a chrome flag to display the browser window.
package main

import (
"context"
"log"
"time"

"github.com/chromedp/chromedp"
)

func main() {
// create chrome instance
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.DisableGPU,
// Set the headless flag to false to display the browser window
chromedp.Flag("headless", false),
)

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

ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

// navigate to a page, wait for some time and then exit.
err := chromedp.Run(ctx,
chromedp.Navigate(`https://xkcd.com/353/`),
chromedp.Sleep(30*time.Second),
)
if err != nil {
log.Fatal(err)
}
}