获取 javascript 渲染页面的 html(与它交互后)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10144820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:49:58  来源:igfitidea点击:

Get the html of the javascript-rendered page (after interacting with it)

javascripthtmlstatesave

提问by user420667

I would like to be able to save the state of the html page after I've interacted with it.

我希望能够在与 html 页面交互后保存它的状态。

Say I click a checkbox, or the javascript set the values of various elements.

假设我单击了一个复选框,或者 javascript 设置了各种元素的值。

How can I save the "javascript-rendered" page?

如何保存“javascript-rendered”页面?

Thanks.

谢谢。

采纳答案by GillesC

That should do and will grab the ALL page not just the body

那应该做并且会抓取所有页面而不仅仅是正文

console.log(document.getElementsByTagName('html')[0].innerHTML);

回答by Grant

In Chrome (and apparently Firefox), there is a special copy() method that will copy the rendered content to the clipboard. Then you can do whatever you want by pasting it to your preferred text editor.

在 Chrome(显然是 Firefox)中,有一个特殊的 copy() 方法可以将渲染的内容复制到剪贴板。然后您可以通过将其粘贴到您喜欢的文本编辑器来做任何您想做的事情。

https://developers.google.com/chrome-developer-tools/docs/commandline-api#copyobject

https://developers.google.com/chrome-developer-tools/docs/commandline-api#copyobject

Console Example:

控制台示例:

copy(document.body.innerHTML);

Note: I noticed Chrome reports undefined after the method is run, however, it seems to execute correctly and the right content is in the clipboard.

注意:我注意到 Chrome 在方法运行后报告未定义,但是,它似乎正确执行并且正确的内容在剪贴板中。

回答by jfriend00

document.body.innerHTMLwill get you the HTML representation of the current document body.

document.body.innerHTML将为您提供当前文档正文的 HTML 表示。

That will not necessarily include all internal state of DOM objects because the HTML contains the initial default state of objects, not necessarily the state that they may have been changed to. The only way to guarantee you get all that state is to make a list of what state you want to save and actually programmatically get that state.

这不一定包括 DOM 对象的所有内部状态,因为 HTML 包含对象的初始默认状态,不一定是它们可能已更改到的状态。保证您获得所有状态的唯一方法是列出您要保存的状态并实际以编程方式获得该状态。

To answer the part of your question about saving it, you'll have to describe more about what problem you're really trying to solve.

要回答关于保存它的部分问题,您必须更多地描述您真正要解决的问题。