在其他浏览器自动化框架中,往往有一个“查找”方法,允许用户查找与选择器匹配的给定元素的所有后代,例如:
https://www.w3schools.com/jquery/jquery_traversing_descendants.asp
$(document).ready(function(){
$("div").find("span");
});
上述方法返回从给定 div
降序匹配 span
的所有元素。
如果我有一个 ElementHandle,有没有办法可以使用 puppeteer 找到与给定选择器匹配的所有依赖项?
最佳答案
是的,您可以使用elementHandle.$
功能。引用文档:
The method runs
element.querySelector
within the page. If no element matches the selector, the return value resolves tonull
.
代码示例:
const elementHandle = await page.$('div');
const elementInsideHandle = await elementHandle.$('span');
如果要查询多个元素,还有$$
函数在页面内运行 element.querySelectorAll
。
关于javascript - 如何查找选定元素或 ElementHandle 的后代元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56224484/