go-rod / rod

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

Get browser console errors #981

Open ryooxxx opened 7 months ago

ryooxxx commented 7 months ago

Rod Version: v0.114.5

package main

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

func main() {
    u := "https://ubuntu.com/core"

    page := rod.New().MustConnect().MustPage()

    done := make(chan int)
    go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { 
        fmt.Println(page.MustObjectsToJSON(e.Args)) 
        close(done) 
    })() 

    page.Timeout(30 * time.Second).MustNavigate(u).MustWaitNavigation()
}

I would like to get the browser error logs/events such as ERR_NAME_NOT_RESOLVED when browsing URLs with the associated loaded network ressource URL. I can see an issue with the same question here https://github.com/go-rod/rod/issues/330, but the code sample above doesn't work. What am I doing wrong ? Thank you !

ysmood commented 7 months ago

too fast to see the log before it closes

ryooxxx commented 7 months ago

package main

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

func main() {
    u := "https://ubuntu.com/core"

    page := rod.New().MustConnect().MustPage()

    done := make(chan int)
    go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { 
        fmt.Println(page.MustObjectsToJSON(e.Args)) 
        close(done) 
    })() 

    page.Timeout(30 * time.Second).MustNavigate(u).MustWaitNavigation()

    log.Printf("waiting 10 sec")
    time.Sleep(10 * time.Second)
    log.Printf("finished waiting")
}

Even with delay, I only see the JS console.log. I would like to see the errors and warnings like ERR_NAME_NOT_RESOLVED. How can I do that ? Thank you !

ysmood commented 7 months ago

I can't get this error with the code you provide. How to reproduce ERR_NAME_NOT_RESOLVED?

ryooxxx commented 7 months ago

Thanks for your answer. It was just an example. Here is a real example. The URL https://helpdesk.paessler.com/en/support/home returns all the errors below.

image

How can I get the same console messages with go-rod ?

package main

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

func main() {
    u := "https://helpdesk.paessler.com/en/support/home"

    page := rod.New().MustConnect().MustPage()

    done := make(chan int)
    go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { 
        fmt.Println(page.MustObjectsToJSON(e.Args)) 
        close(done) 
    })() 

    page.Timeout(30 * time.Second).MustNavigate(u).MustWaitNavigation()

    log.Printf("waiting 10 sec")
    time.Sleep(10 * time.Second)
    log.Printf("finished waiting")
}

Thank you for your time

joao-pinheiro commented 3 weeks ago

To capture the browser log, add a handler for proto.LogEntryAdded events:

go page.EachEvent(func(e *proto.LogEntryAdded) { 
     msg, _ := json.Marshal(e.Entry)
     fmt.Println(string(msg))
})()