javascript - page.evaluate 和 page.$ 的不同行为具有后续功能
问题描述
我有一个要加载的页面。以下page.$eval()
代码有效:
const productName = await page.$eval('h1[role="main"]', el =>
[].reduce.call(
el.childNodes,
(a, b) =>
a +
(b.nodeType === 3
? b.textContent
.replace(/[\r\n]+/gm, "")
.replace(/\s+/g, " ")
.trim()
: ""),
""
)
);
通过作品,我的意思是它返回预期的产品名称。我在主函数中运行它。
我现在想将代码外包给原始上下文之外的自己的函数(以供重用)。
我在主函数中调用函数,如下所示:
const productName = await page
.$('h1[role="main"]')
.then(el => getProductNameFromSelector(el))
.catch(err => console.log("failure product name", err));
外包的功能是这样的:
const getProductNameFromSelector = async el =>
el
.evaluate(
el,
[].reduce.call(
el.childNodes,
(a, b) =>
a +
(b.nodeType === 3
? b.textContent
.replace(/[\r\n]+/gm, "")
.replace(/\s+/g, " ")
.trim()
: ""),
""
)
)
.then(result => result)
.catch(err => console.log("error in function", err, el));
它遇到以下错误:
failure product name TypeError: Cannot read property 'evaluate' of null
at reduce (<anonymous>)
at getProductNameFromSelector (pathToFile.js:395:17)
at page.$.then.el (pathToFile.js:119:21)
at process._tickCallback (internal/process/next_tick.js:68:7)
我应该收到一个ElementHandle
from page.$('h1[role="main"]')
。它undefined
虽然说。
解决方案
evaluate
函数需要期望元素作为el
参数
const getProductNameFromSelector = async el =>
el
.evaluate(el => //HERE
[].reduce.call(
el.childNodes,
(a, b) =>
a +
(b.nodeType === 3
? b.textContent
.replace(/[\r\n]+/gm, "")
.replace(/\s+/g, " ")
.trim()
: ""),
""
)
)
.then(result => result)
.catch(err => console.log("error in function", err, el));
您也可以等待选择器:
const productName = await page
.waitForSelector('h1[role="main"]')
.then(el => getProductNameFromSelector(el))
.catch(err => console.log("failure product name", err));
推荐阅读
- html - 图像未显示在 Rich Snippet / Google Schema 中
- ios - 如何在 SkyFloatingLabelTextField 中垂直和水平对齐文本?
- javascript - Javascript没有选择稍后通过javascript使用innerHTML添加的html元素
- vue.js - 如何使用 Vue.Js 从 Store.js 访问 Mixin.js 文件的 $data 变量?
- compiler-errors - 目标文件中未定义的符号
- javascript - Rails 6 webpacker在javascript文件中使用环境变量
- java - 当我尝试使用循环在 excel 工作表中写入行和列时,仅打印最后一个循环值
- spring - Spring Batch Job 实例 ID 有时不会生成
- python - 用命令自动制作按钮
- android - Android 蓝牙 API 与配对手机提供的服务