go-rod / rod

A Chrome DevTools Protocol driver for web automation and scraping.
https://go-rod.github.io
MIT License
5.23k stars 344 forks source link

Screenshot of img element is blank #321

Closed dataflowjs closed 3 years ago

dataflowjs commented 3 years ago

Rod Version: v0.85.9

Im trying to make captcha img screenshot but result is blank... why? Minimal example:

package main

import (
    "log"
    "time"

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

func main() {
    ln := launcher.New().
        Set("no-sandbox", "true").
        Headless(false)
    defer ln.Cleanup()

    ctl, err := ln.Launch()
    if err != nil {
        log.Println("launcher")
        return
    }
    browser := rod.New().
        ControlURL(ctl).
        Trace(true).
        SlowMotion(250 * time.Millisecond)

    err = browser.Connect()
    if err != nil {
        log.Println("connect")
        return
    }

    defer browser.MustClose()

    var page *rod.Page

    err = rod.Try(func() {
        page = browser.
            MustIncognito().
            MustPage("https://rosreestr.gov.ru/wps/portal/online_request").
            MustSetViewport(1366, 768, 1, false).
            MustWindowMaximize()
    })

    if err != nil {
        log.Println("cannot open page")
        return
    }

    err = page.Timeout(240 * time.Second).WaitLoad()
    if err != nil {
        log.Println("load")
        return
    }

    eln := "img#captchaImage2"
    el, err := page.Timeout(10 * time.Second).Element(eln)
    if err != nil {
        log.Println("cannot find element", eln)
        return
    }

    el.MustScreenshot("img.png")

    utils.Pause()
}
gh-robot commented 3 years ago

Please add a valid **Rod Version:** v0.0.0 to your issue. Current version is v0.85.9 generated by check-issue

ysmood commented 3 years ago

Seems like it's a limitation of Chrome: https://github.com/puppeteer/puppeteer/issues/2357

Change el.MustScreenshot("img.png") to utils.OutputFile("img.png", el.MustResource()) will work.