Puppeteer:如何在不使用评估的情况下获取父节点?

Viv*_*ipo 3 javascript acceptance-testing node.js google-chrome-devtools puppeteer

通过 Puppeteer,我知道如何使用评估来获得一些属性,例如parentNodeor previousSibling

let id = await page.evaluate(() => {
    let wantedHref = $('li a').filter(
        function (index) {
            return $(this).text().includes("Text that I Want");
        })[0];
    //get parentNode
    let id = wantedHref.parentNode.parentNode.parentNode.id;
    //get  previousSibling
    let expandIcon = wantedLink.parentNode.parentNode.previousSibling;
    expandIcon.click();
    return id;
});
Run Code Online (Sandbox Code Playgroud)

我想知道如何在不使用评估的情况下检索这些类型的属性。

请问你能帮帮我吗?

Gra*_*ler 7

elementHandle.getProperty('parentNode')

您可以使用elementHandle.getProperty()来获取parentNode当前的属性ElementHandle

const current_element = await page.$('#example');
const parent_node = await current_element.getProperty('parentNode');

await parent_node.click();
Run Code Online (Sandbox Code Playgroud)