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

如何获取js脚本执行渲染后html #658

Closed
feeops opened this issue Jul 15, 2022 · 3 comments
Closed

如何获取js脚本执行渲染后html #658

feeops opened this issue Jul 15, 2022 · 3 comments
Labels
question Questions related to rod

Comments

@feeops
Copy link

feeops commented Jul 15, 2022

Rod Version: v0.108.1

现在好多网页是用ajax获取数据再填充到网页中,想获取渲染后的代码,不是原始源代码
类似于chrome检查的功能中代码

在chromedp是这样实现的

	if err := chromedp.Run(ctx,
		chromedp.Navigate(url),
		chromedp.Sleep(time.Duration(Interval)*time.Second),
		chromedp.OuterHTML("html", &pageHtml, chromedp.ByQuery),
	); err != nil {
		log.Fatal(err)
	}

我尝试这样使用

page := browser.MustPage(startURL)
utils.Sleep(10)
html = page.MustElement("html").MustHTML()

但这里返回的原始html代码,不是渲染之后的

@feeops feeops added the question Questions related to rod label Jul 15, 2022
@feeops feeops changed the title 如何获取jse脚本执行渲染后html 如何获取js脚本执行渲染后html Jul 15, 2022
@passerbyo
Copy link

同问

@ysmood
Copy link
Member

ysmood commented Jul 15, 2022

rod/page_test.go

Lines 138 to 140 in c78f6bd

p := g.page.MustNavigate(g.srcFile("fixtures/click.html")).MustWaitLoad()
p.MustElement("button").MustClick()
g.Has(p.MustHTML(), `a="ok"`)

单元测试可以看到 MustHTML 返回的就是渲染后的内容,只有点击了 button 后页面才可能出现 a="ok"

@ysmood ysmood closed this as completed Jul 15, 2022
@feeops
Copy link
Author

feeops commented Jul 15, 2022

测试了,是没有问题了,我的代码中之前有使用browser.HijackRequests,有一些问题导致html是空白的

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

3 participants