node.js - puppeteer 操纵者的 ElementHandle.getProperty() 的预期行为是什么?

标签 node.js puppeteer

Puppeteer 1.0.0-发布。 getProperty() 方法似乎有些神奇。例如,如果您的页面包含:

<a href="/foo/bar.html">link</a>

然后返回的不是相对而是绝对 URL:

const propertyHandle = await elementHandle.getProperty('href');
const href = await propertyHandle.jsonValue();
// href is 'https://localhost:8080/foo/bar.html'

另一方面,如果你要绕更多的路:

const hrefHandle = await page.evaluateHandle(element => element.getAttribute('href'), elementHandle);
const href = await hrefHandle.jsonValue();
// href is '/foo/bar.html'

据我所知,puppeteer 文档没有提到 getProperty() 的这种行为?

它变得更丑陋,例如,如果你想获得元素的 style 属性。看起来 puppeteer 的 getProperty() 实际上试图以某种方式解析样式,但解析存在错误/不完整。获取原始文本的唯一方法是通过迂回调用 evaluateHandle(...)

这是一个预期的功能,而仅仅是一个文档错误吗?还是完全是 puppeteer 错误?

谢谢。

最佳答案

参见 HTML - attributes vs properties HTML 属性和 DOM 属性之间的区别。

您也可以在没有 Puppeteer 的情况下轻松看出差异。例如,在此页面上:

document.getElementById('nav-questions').href    
// returns "https://stackoverflow.com/questions"

document.getElementById('nav-questions').getAttribute('href')    
// returns "/questions"

关于node.js - puppeteer 操纵者的 ElementHandle.getProperty() 的预期行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48654244/

相关文章:

encryption - 在 Node.JS 中加密和解密持久数据的最佳方式?

travis-ci - Puppeteer Travis-CI chrome headless 不工作

google-chrome-devtools - 同时运行多个 Puppeteer 实例是否安全?

ios - 在 iOS 上运行 node.js 库

javascript - 从 url 调用 Nodejs 函数

node.js - puppeteer:在 DevTools 的网络选项卡中访问特定请求的 JSON 响应

docker - Puppeteer 错误 : error while loading shared libraries: libgobject-2. 0.so.0

javascript - 如何在 Node js 中执行类似于 goto 语句的操作,或者如何在异步函数中创建和调用函数?

node.js - 如何在谷歌应用程序引擎中为 Node react 项目配置app.yaml文件

javascript - 如何导出 multer 模块