Pipo
Pipo

Reputation: 5611

Puppeteer: How to get parentNode without using evaluate?

With Puppeteer, I know how to use evaluate for having some properties like parentNode or 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;
});

I would like to know how I could retrieve these types of properties without using evaluation.

Could you help me, please?

Upvotes: 3

Views: 4610

Answers (1)

Grant Miller
Grant Miller

Reputation: 29037

elementHandle.getProperty('parentNode')

You can use elementHandle.getProperty() to obtain the parentNode property of the current ElementHandle:

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

await parent_node.click();

Upvotes: 7

Related Questions