本文介绍了如何获得剧作家的元素集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取剧作家页面上的所有图像?我只能通过以下代码获得一个 (ElementHandle
),但不能获得一个集合.
How to get all images on the page with playwright?I'm able to get only one (ElementHandle
) with following code, but not a collection.
const { chromium } = require("playwright");
class Parser {
async parse(url) {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(url);
await page.waitFor("img");
// TODO: get somehow collection of elements
return await page.$("img");
}
}
module.exports = Parser;
在很远的另一个模块的某处:
Somewhere in another module far far away:
const Parser = require("./path/to/dir/Parser.js");
const parser = new Parser();
parser
.parse(body.url)
.then(elemHandle => {
// here I get only one ElementHandle object, but suppose to get an array or collection
})
.catch(err => {
throw new Error(err);
});
节点 v.12.16.1
Node v.12.16.1
推荐答案
我已经找到了答案.需要使用 page.$$(selector)
而不是 page.$(selector)
来抓取,就像 document.querySelectorAll(selector)
.
I have already found the answer. Need to use page.$$(selector)
instead of page.$(selector)
to grab like document.querySelectorAll(selector)
.
这篇关于如何获得剧作家的元素集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!