#1034·rod

Screenshot in Emulate error

Author: 8763232Created Apr 2, 2024Updated Sep 8, 2026
Labelsenhance
go
// Screenshot of the area of the element
func (el *Element) Screenshot(format proto.PageCaptureScreenshotFormat, quality int) ([]byte, error) {
	err := el.ScrollIntoView()
	if err != nil {
		return nil, err
	}

	opts := &proto.PageCaptureScreenshot{
		Quality: gson.Int(quality),
		Format:  format,
	}
        //page full Screenshot   Notice :The device resolution is inconsistent with the display resolution
	bin, err := el.page.Context(el.ctx).Screenshot(false, opts)    
	if err != nil {
		return nil, err
	}

	// so that it won't clip the css-transformed element
	shape, err := el.Shape()
	if err != nil {
		return nil, err
	}

	box := shape.Box()   // get el box 

	// TODO: proto.PageCaptureScreenshot has a Clip option, but it's buggy, so now we do in Go.
        // cut the Screenshot ,but this box x/y, Width/Height are inconsistent with the resolution. Will cause screenshot error
       //   window.devicePixelRatio  is not 1,in Emulate that is 3
	return utils.CropImage(bin, quality,  
		int(box.X),
		int(box.Y),
		int(box.Width),
		int(box.Height),
	)
}

Rod Version: v0.114.6

Need to recalculate coordinates to take screenshot correctly