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 to listen for EventAuthRequired events correctly #654
Comments
对于文档中的这个例子中的 http 地址确实是可行的,但将 |
怎么才能正确订阅 |
这个就是switch-case,IDE跳转到函数文档就够了: https://go-rod.github.io/#/events/README?id=handle-multiple-events |
但是监听了 |
这个是你代理的问题,http://mdn.dev 会强制跳转到 https |
我的测试方式:
|
什么网站,单元测试你都无法通过吗?我这边测试是没有问题的 Lines 348 to 382 in c78f6bd
|
我也尝试了以下单元测试,均能正常加载 html 元素
依然会超时失败的测试: 将如下行 Lines 370 to 371 in c78f6bd
更换为 page := g.newPage("https://mdn.dev")
el := page.MustElement("html") 正常获取 html 元素的方式 : Line 368 in c78f6bd
疑问 & 不解: 不能为正常页面监听 |
可以的,给个 for loop 就行了,只要出现 auth 事件就处理。
我之前不是说了问题出在 https 吗?你只改 http 为 https,为啥不改改代理的设置呢,都要改的不能只改一个。我测试里的代理是为 http 设计的,没有考虑 https 的情况。这也不是 rod 该要解决的问题,代理的问题请自己查看专注于代理的库或者教学。
你说 http 也有同样问题,能给个只有 http 的网站吗?或者自己mock 一个只有 http 的,用 http://mdn.dev 是不行的,它会强制跳转到 https 页面。 |
感谢作者这么耐心解答问题,很抱歉我还是有些地方不明白
新问题: 一定要设置代理才可以正确监听 auth 吗,对于 http://mdn.dev 我去掉了代理也是空白页面。
|
不用,这两个没有关系。 你这个网站打开不需要 auth 啊?不懂你给这个地址什么意思? |
就是这个 ISSUE 最开始的问题,动态爬虫中,我不知道哪些页面需要处理 Auth 事件,所以想让所有页面都监听 Auth 事件,但正常页面监听这个事件的话就会阻塞页面加载,参照 crawlergo 调用 chromdp 的使用方式改写的 |
懂你意思了,看了下代码,原因是你抄漏了这行: 如果要处理 fetch 事件,必须要处理所有的 pause 事件,这个 chrome 的文档里有提到。我写个例子给你看看就懂了,注意看 func TestLab(t *testing.T) {
page := rod.New().MustConnect().MustPage()
go page.EachEvent(func(e *proto.FetchAuthRequired) {
fakeAuth := &proto.FetchAuthChallengeResponse{
Response: proto.FetchAuthChallengeResponseResponseProvideCredentials,
Username: "user",
Password: "passwd123",
}
_ = proto.FetchContinueWithAuth{
RequestID: e.RequestID,
AuthChallengeResponse: fakeAuth,
}.Call(page)
}, func(e *proto.FetchRequestPaused) {
_ = proto.FetchContinueRequest{
RequestID: e.RequestID,
}.Call(page)
})()
txt := page.MustNavigate("https://mdn.dev").
MustElement("#root > div > main > div > div:nth-child(1) > section:nth-child(1) > h2").
MustText()
gop.P(txt)
} |
感谢大佬! 怪我一开始表述不清,哈哈 |
Rod Version: v0.107.3
The code to demonstrate your question
Hi guys, sorry for my english (and thanks to google translator)
as for me it's much nicer than chromedp, but I have a problem
I want to listen for
FetchAuthRequired
events, but such code will block the page. I also did not find relevant examples.how to use
EachEvent
forFetchAuthRequired
event correctly.for my error code
I tried the example but it was blank
rod/examples_test.go
Lines 350 to 369 in 8eb4347
What you got
blank page for my code and timeout panic
blank page for
rod/example_test.go
What you expected to see
the page loads normally just like chromedp listenTarget
What have you tried to solve the question
Try to understand function source code of
HandleAuth()
& compare chromedp code with rod codeThe text was updated successfully, but these errors were encountered: