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

how to display browser interactions as chromedp is running? #311

Closed
terrybytes opened this issue Apr 21, 2019 · 12 comments
Closed

how to display browser interactions as chromedp is running? #311

terrybytes opened this issue Apr 21, 2019 · 12 comments

Comments

@terrybytes
Copy link

What versions are you running?

latest

go list -m: not using modules
Chromium 73.0.3683.103 snap
go version go1.12.1 linux/amd64/pre>

#### What did you do?

ran the examples


#### What did you expect to see?

a browser 


#### What did you see instead?

nothing
@terrybytes
Copy link
Author

When using an earlier version i could see the browser as my chromedp prog was running, after git-pulling the lastest version i seem to be in headless mode. How do i turn this off?
thnx

@mvdan
Copy link
Contributor

mvdan commented Apr 21, 2019

Headless is a default option. To use other options, see the examples: https://godoc.org/github.com/chromedp/chromedp#example-ExecAllocator

@mvdan mvdan closed this as completed Apr 21, 2019
@terrybytes
Copy link
Author

took a quick look, what's the opposite of "chromedp.Headless" ?

@mvdan
Copy link
Contributor

mvdan commented Apr 21, 2019

It's the --headless flag, so if you don't want it, remove that line.

@terrybytes
Copy link
Author

thnx much

@burque505
Copy link

@mvdan wrote: "It's the --headless flag, so if you don't want it, remove that line."
Where is that line? Thanks.
@mvdan wrote: "Headless is a default option. To use other options, see the examples: https://godoc.org/github.com/chromedp/chromedp#example-ExecAllocator"
That example is not instructive as to how to NOT run headless. Thanks.

@ZekeLu
Copy link
Member

ZekeLu commented Feb 4, 2022

@burque505 See #845

@burque505
Copy link

burque505 commented Feb 4, 2022

Thank you, @ZekeLu. From #845 I see the issue was certainly recognized - I guess the change is not yet implemented (or perhaps was rolled back?) as I received this working answer on the Discord yesterday:

ctx, cancel = chromedp.NewExecAllocator(ctx, append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false))...)
defer cancel()

@ZekeLu
Copy link
Member

ZekeLu commented Feb 4, 2022

@burque505 It has been implemented, but not merged yet.

@burque505
Copy link

Ahah! Thanks so much, I really appreciate your help. Now I understand.

@Alechan
Copy link

Alechan commented Feb 5, 2023

Why is my script running out of time when having headless = false?

For example, the screenshot script from the official examples finishes if I run it without making any changes.

But if I adapt it to run with headless = false, the context is canceled and the execution stopped.

func main() {
    opts := append(chromedp.DefaultExecAllocatorOptions[:],
        chromedp.Flag("headless", false),
    )
    ctx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
    // create context
    ctx, cancel := chromedp.NewContext(
        ctx,
        // chromedp.WithDebugf(log.Printf),
    )
    defer cancel()

    // capture screenshot of an element
    var buf []byte
    if err := chromedp.Run(ctx, elementScreenshot(`https://pkg.go.dev/`, `img.Homepage-logo`, &buf)); err != nil {
        log.Fatal(err)
    }
    if err := os.WriteFile("elementScreenshot.png", buf, 0o644); err != nil {
        log.Fatal(err)
    }

    // capture entire browser viewport, returning png with quality=90
    if err := chromedp.Run(ctx, fullScreenshot(`https://brank.as/`, 90, &buf)); err != nil {
        log.Fatal(err)
    }
    if err := os.WriteFile("fullScreenshot.png", buf, 0o644); err != nil {
        log.Fatal(err)
    }

    log.Printf("wrote elementScreenshot.png and fullScreenshot.png")
}

// elementScreenshot takes a screenshot of a specific element.
func elementScreenshot(urlstr, sel string, res *[]byte) chromedp.Tasks {
    return chromedp.Tasks{
        chromedp.Navigate(urlstr),
        chromedp.Screenshot(sel, res, chromedp.NodeVisible),
    }
}

// fullScreenshot takes a screenshot of the entire browser viewport.
//
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// device.Reset to reset the emulation and viewport settings.
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
    return chromedp.Tasks{
        chromedp.Navigate(urlstr),
        chromedp.FullScreenshot(res, quality),
    }
}

@ZekeLu
Copy link
Member

ZekeLu commented Feb 6, 2023

@Alechan I can not reproduce the issue. Please file a new issue and fill the issue template. Thank you!

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

5 participants