go-rod / rod

A Chrome DevTools Protocol driver for web automation and scraping.

Home Page:https://go-rod.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can I check Internet activity

duolabmeng6 opened this issue · comments

Since I didn't find an example of blocking network requests in the example, let me ask that I want to see all network activity in the browser

Please add a valid Rod Version: v0.0.0 to your issue. Current version is v0.114.5

generated by check-issue

package main

import (
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

func main() {
	url := launcher.New().Headless(false).MustLaunch()
	browser := rod.New().ControlURL(url).MustConnect()
	page := browser.MustPage("")

	go func() {
		page.EachEvent(func(e *proto.NetworkRequestWillBeSent) {
			fmt.Printf("Request: %s %s\n", e.Request.Method, e.Request.URL)
		}, func(r *proto.NetworkResponseReceived) {
			//How do I get the contents of the body here
			//m := proto.NetworkGetResponseBody{RequestID: r.RequestID}
			//r, _ = m.Call(page)

		})()
	}()
	time.Sleep(3 * time.Second)

	page.MustNavigate("https://www.baidu.com")

	select {}
}

How do I get the contents of the body here

I hope it helps others

package main

import (
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

func main() {
	url := launcher.New().Headless(false).MustLaunch()
	browser := rod.New().ControlURL(url).MustConnect()
	page := browser.MustPage("")

	go func() {
		page.EachEvent(func(e *proto.NetworkRequestWillBeSent) {
			fmt.Printf("Request: %s %s\n", e.Request.Method, e.Request.URL)
		}, func(e *proto.NetworkResponseReceived) {
			reply, err := (proto.NetworkGetResponseBody{RequestID: e.RequestID}).Call(page)
			if err != nil {
				fmt.Println(err)
			}
			fmt.Println(reply.Body)
		})()
	}()
	time.Sleep(3 * time.Second)
	page.MustNavigate("https://www.baidu.com")
	select {}
}