Skip to content

Screenshot Hangs on Non-Activated Tabs in Both Headless & Non-Headless Modes #1211

@Dastagirireddy

Description

@Dastagirireddy

Rod Version: v0.116.2

The code to demonstrate your question

package main

import (
	"log"
	"os"

	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
)

func main() {
	// Launch browser in non-headless mode
	url := launcher.New().Headless(false).MustLaunch()

	browser := rod.New().ControlURL(url).MustConnect()
	defer browser.MustClose()

	page1 := browser.MustPage("about:blank")
	page2 := browser.MustPage("about:blank") // multiple tabs

	pages := []*rod.Page{page1, page2}

	page := pages[0]

	page.MustNavigate("https://google.com").MustWaitLoad()

	// Confirm page is loaded (DOM snapshot works fine)
	log.Println("DOM snapshot captured")

	// This call hangs indefinitely unless page1.MustActivate() is used before
	screenshot, err := page.Screenshot(false, nil)
	if err != nil {
		log.Fatalf("Failed to take screenshot: %v", err)
	}

	// Save screenshot to file
	err = os.WriteFile("screenshot.png", screenshot, 0644)
	if err != nil {
		log.Fatalf("Failed to save screenshot: %v", err)
	}

	log.Println("✅ Screenshot saved to screenshot.png")
}

What you got

Calling .Screenshot() on a non-active page (i.e., not the most recently opened tab) causes the call to hang indefinitely - even in headless mode.

What you expect to see

Screenshot should be captured successfully regardless of whether the page is focused or not — similar to Puppeteer or Playwright behavior.

What have you tried to solve the question

  1. Verified that page1.MustCaptureDOMSnapshot() works correctly -- proves the page is rendered.
  2. Added page1.MustActivate() before .Screenshot() -- fixes the issue.
  3. Tried MustScrollIntoView() -- had no effect
  4. Happens consistently in both headless and non-headless modes
  5. Implemented serialization + manual focus workaround, but behavior is surprising

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestions related to rod

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions