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

使用协程处理多页面,截图失败 #1096

Closed
DC3x6 opened this issue Jul 20, 2024 · 3 comments
Closed

使用协程处理多页面,截图失败 #1096

DC3x6 opened this issue Jul 20, 2024 · 3 comments
Labels
question Questions related to rod

Comments

@DC3x6
Copy link
Contributor

DC3x6 commented Jul 20, 2024

Rod Version: v0.116.2

问题描述

通过开启多协程来访问网站并截图时,截图会失败,如果协程数量为1则不会。

示例代码

package main

import (
	"errors"
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"github.com/remeh/sizedwaitgroup"
	"github.com/ysmood/leakless/pkg/utils"
	"log"
	"strings"
	"time"
)

func main() {
	url := []string{
		"https://cn.bing.com/",
		"https://www.baidu.com/",
		"https://www.qq.com/",
		"https://www.163.com/",
		"https://www.sohu.com/",
		"https://www.sogou.com/",
		"https://www.youku.com/"}
	b, err := New()
	if err != nil {
		log.Fatal(err)
	}
	defer b.Close()

	wg := sizedwaitgroup.New(3)
	for _, u := range url {
		wg.Add()
		go func(u string) {
			defer wg.Done()
			page, err := b.Page(proto.TargetCreateTarget{})
			if err != nil {
				log.Printf("could not create target, url=%s, err=%s", u, err)
			}
			defer page.Close()

			//hijack
			//do something

			timeout := 30 * time.Second

			page = page.Timeout(timeout)

			waitNavigation := page.WaitNavigation(proto.PageLifecycleEventNameFirstMeaningfulPaint)

			err = page.Navigate(u)
			if err != nil {
				log.Printf("could not navigate target, url=%s, err=%s", u, err)
			} else {
				waitNavigation()
			}

			page.WaitLoad()

			w := page.WaitRequestIdle(1*time.Second, nil, nil, nil)

			w()
			println(u)
			println("TargetID: " + page.TargetID)
			println("FrameID: " + page.FrameID)
			println("SessionID: " + page.SessionID)

			bin, err := page.Screenshot(true, nil)
			if err != nil {
				log.Printf("could not take screenshot, url=%s, err=%s", u, err)
			}
			path := fmt.Sprintf("screenshots/%s.png", strings.SplitN(u, "/", 4)[2])
			err = utils.OutputFile(path, bin, nil)
			if err != nil {
				log.Printf("could not save screenshot, url=%s, err=%s", u, err)
			}

		}(u)

	}
	wg.Wait()

}

func New() (*rod.Browser, error) {
	chromeLauncher := launcher.New().
		Leakless(false).
		Append("disable-infobars", "").
		Append("disable-extensions", "").
		Set("disable-web-security").
		Set("allow-running-insecure-content").
		Set("reduce-security-for-testing").
		Set("disable-gpu", "true").
		Set("ignore-certificate-errors", "true").
		Set("ignore-certificate-errors", "1").
		Set("disable-crash-reporter", "true").
		Set("disable-breakpad", "true").
		Set("disable-notifications", "true").
		//Set("window-size", fmt.Sprintf("%d,%d", 1080, 1920)).
		Set("hide-scrollbars", "true").
		Set("mute-audio", "true").
		Set("incognito", "true").
		Bin("/chrome-mac/Chromium.app/Contents/MacOS/Chromium")
	chromeLauncher.Headless(false)
	//chromeLauncher.Set("engine", "new")

	launcherURL, launcherErr := chromeLauncher.Launch()
	if launcherErr != nil {
		return nil, launcherErr
	}

	browser := rod.New().NoDefaultDevice().ControlURL(launcherURL)
	if browserErr := browser.Connect(); browserErr != nil {
		return nil, browserErr
	}
	incognito, err := browser.Incognito()
	if err != nil {
		chromeLauncher.Kill()
		return nil, errors.New("failed to create incognito browser")
	}

	return incognito, nil
}

协程为1时,截图正常
wg := sizedwaitgroup.New(1)

image

协程为3时,部分截图失败,并且每次截图失败的网站不同
image

@DC3x6 DC3x6 added the question Questions related to rod label Jul 20, 2024
Copy link

Please fix the format of your markdown:

139:1 MD033/no-inline-html Inline HTML [Element: img]
142:1 MD033/no-inline-html Inline HTML [Element: img]

generated by check-issue

@Fly-Playgroud
Copy link
Contributor

建议开启 headlless model 截图试试

@Fly-Playgroud
Copy link
Contributor

在 headless 模式下应该使得页面中的元素具备了“可见性”“可交互性”等,即使用户不处于当前页面
但是当处于 headful 模式下只有用户处于的这个页面中可视区域的元素才具有“可见性”和“可交互性”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants