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 can I get continuous perfect images using screenshots #1445

Open
wjuuuuuu opened this issue Mar 12, 2024 · 0 comments
Open

How can I get continuous perfect images using screenshots #1445

wjuuuuuu opened this issue Mar 12, 2024 · 0 comments

Comments

@wjuuuuuu
Copy link

wjuuuuuu commented Mar 12, 2024

What versions are you running?

$ go list -m github.com/chromedp/chromedp
github.com/chromedp/chromedp v0.9.5

$ google-chrome --version

$ go version
go version go1.21.3 windows/amd64


#### What did you do? Include clear steps.
1. Put specific URL  with specific tag 
2. Run Do screen-shot function



func main() {
ctx, cancel := chromedp.NewExecAllocator(context.Background(),
append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("blink-settings", "imagesEnabled=true"),
chromedp.WindowSize(1920, 200000),
))
defer cancel()
ctx, cancel = chromedp.NewContext(ctx, chromedp.WithDebugf(log.Printf))
defer cancel()

var wg sync.WaitGroup
wg.Add(1)
lctx, cancel := context.WithCancel(ctx)
defer cancel()
chromedp.ListenTarget(lctx, func(ev interface{}) {
	if ev, ok := ev.(*page.EventLifecycleEvent); ok {
		if ev.Name == "InteractiveTime" {
			wg.Done()
			cancel()
		}
	}
})

var overview []*cdp.Node
var nodes []*cdp.Node
err := chromedp.Run(ctx,
	chromedp.Navigate("https://www.lge.co.kr/home-audio/tone-ut90s-black"),
	chromedp.EmulateViewport(800, 200000),
	chromedp.ActionFunc(func(ctx context.Context) error {
		pCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
		defer cancel()
		if err := chromedp.Click("body div.ins-popup-with-image-close-button", chromedp.BySearch).Do(pCtx); err != nil {
			log.Printf("Click Err:%s", err)
		}
		cancel()
		return nil
	}),
	chromedp.Nodes(`overview`, &overview, chromedp.ByID, chromedp.Populate(3, true, chromedp.PopulateWait(1*time.Second))),
	chromedp.Evaluate("let node = document.querySelector('div.floating-chatbot'); node.parentNode.removeChild(node)", nil),
	chromedp.Evaluate("let node2 = document.querySelector('div.floating-wrap'); node2.parentNode.removeChild(node2)", nil),
	chromedp.EmulateViewport(800, 1000),
	chromedp.ActionFunc(func(ctx context.Context) error {
		wg.Wait()

		if len(overview) != 0 && overview[0].ChildNodeCount > 0 {
			if err := chromedp.Nodes(`div.iw_placeholder`, &nodes, chromedp.FromNode(overview[0]), chromedp.Populate(-1, true, chromedp.PopulateWait(1*time.Second))).Do(ctx); err != nil {
				log.Printf("Node ERR:%s", err)
			}
		}
		if err := chromedp.Sleep(3 * time.Second).Do(ctx); err != nil {
			log.Printf("Sleep Error:%s", err)
		}
		for i, node := range nodes[0].Children {

			nodeIdValue := node.AttributeValue("id")
			if err := chromedp.ScrollIntoView(nodeIdValue, chromedp.ByID).Do(ctx); err != nil {
				log.Printf("Scroll Error:%s", err)
			}
			modifyClass(ctx, node)
			if err := chromedp.Sleep(3 * time.Second).Do(ctx); err != nil {
				log.Printf("Sleep Error:%s", err)
			}
			var buf []byte
			if err := chromedp.Screenshot(nodeIdValue, &buf, chromedp.ByID).Do(ctx); err != nil {
				log.Printf("Screenshot ERROR:%s", err)
			}

			strI := strconv.Itoa(i)
			filename := "test2_" + strI + ".png"
			if err := os.WriteFile(filename, buf, 0o644); err != nil {
				log.Fatal(err)
			}

		}

		return nil
	}),
)
if err != nil {
	log.Fatal(err)
}

}


#### What did you expect to see?
1. Get Perfect continuous Images 

#### What did you see instead?
1. Got images which offered by tag with extra spaces 
2. Got images with a disappeared image
@wjuuuuuu wjuuuuuu changed the title how to get clean images How can I get continuous perfect images using screenshots Mar 15, 2024
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

1 participant