Puppeteer.js not able to query DOM after waiting for element

27 Views Asked by At

I am getting an error trying to get a DOM element, trying a variety of ways to select and make sure the element is available prior to selecting.

code:

(async () => {

  let browser = await puppeteer.launch({ headless: true});


  let page = await browser.newPage();
  page.setDefaultTimeout(0)



    await page.goto('https://www.facebook.com', { waitUntil: 'networkidle0'})

    await page.type('#email', 'email', { delay: 15})
    await page.type('#pass', 'password', { delay: 15})
    await page.click('button[name="login"]');

    await page.goto('https://www.facebook.com/marketplace/you/selling/', { waitUntil: 'domcontentloaded', timeout: 90000});


    let sellingMarketplaceList = "div[aria-label='Collection of your marketplace items']"

    await page.waitForSelector(sellingMarketplaceList, {visible: true})

    let element = await page.$(sellingMarketplaceList)
    let value = await page.evaluate(el => el.textContent, element)

    await page.waitForSelector(sellingMarketplaceList, {visible: true})
    const selection = await page.$$eval(sellingMarketplaceList)
    
    // returns an object that mostly just gives the length `selection2 (1) [CdpElementHandle]`
    const selection2 = await page.$$(sellingMarketplaceList);

    const selection3 = page.queryObjects(sellingMarketplaceList)

    console.log(selection)
    console.log(selection2)
    console.log(selection3)
    console.log(value)
})();

errors:

const selection = await page.$$eval(sellingMarketplaceList)

Exception has occurred: TypeError: Cannot convert undefined or null to object
  at hasOwnProperty (<anonymous>)
    at withSourcePuppeteerURLIfNone (/Users/don/dev/javascript/nightmare/node_modules/puppeteer-core/lib/cjs/puppeteer/common/util.js:191:41)
    at CdpPage.$$eval (/Users/don/dev/javascript/nightmare/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Page.js:474:71)
    at /Users/don/dev/javascript/nightmare/puppet.js:74:32
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

const selection3 = page.queryObjects(sellingMarketplaceList)

Exception has occurred: Error: Prototype JSHandle must not be referencing primitive value
  at assert (/Users/don/dev/javascript/nightmare/node_modules/puppeteer-core/lib/cjs/puppeteer/util/assert.js:18:15)
    at CdpPage.queryObjects (/Users/don/dev/javascript/nightmare/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Page.js:458:32)
    at /Users/don/dev/javascript/nightmare/puppet.js:76:27
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

My goal is to have the element I am selecting in a state that I can parse further.

0

There are 0 best solutions below