Skip to content
Advertisement

Puppeteer , bringing back blank array

I’m trying to grab products from ebay and open them on amazon.

So far, I have them being searched on amazon but I’m struggling with getting the products selected from the search results.

Currently its outputting a blank array and im not sure why. Have tested in a separate script without the grabTitles and the for loop. So im guessing there is something in that causing an issue.

Is there something i am missing here thats preventing the data coming back for prodResults?

JavaScript

Advertisement

Answer

There are a few potential problems with the script:

  1. await page.keyboard.press('Enter'); triggers a navigation, but your code never waits for the navigation to finish before trying to select the result elements. Use waitForNavigation, waitForSelector or waitForFunction (not waitForTimeout).

    If you do wait for a navigation, there’s a special pattern using Promise.all needed to avoid a race condition, shown in the docs.

    Furthermore, you might be able to skip a page load by going directly to the search URL by building the string yourself. This should provide a significant speedup.

  2. Your code spawns a new page for every item that needs to be processed, but these pages are never closed. I see grabTitles.length as 60. So you’ll be opening 60 tabs. That’s a lot of resources being wasted. On my machine, it’d probably hang everything. I’d suggest making one page and navigating it repeatedly, or close each page when you’re done. If you want parallelism, consider a task queue or run a few pages simultaneously.

  3. grabTitles[i++] — why increment i here? It’s already incremented by the loop, so this appears to skip elements, unless your selectors have duplicates or you have some other reason to do this.

  4. span.a-size-medium doesn’t work for me, which could be locality-specific. I see a span.a-size-base-plus.a-color-base.a-text-normal, but you may need to tweak this to taste.

Here’s a minimal example. I’ll just do the first 2 items from the eBay array since that’s coming through fine.

JavaScript

Output:

JavaScript

Note that I added user agents and headers to be able to use headless: true but it’s incidental to the main solution above. You can return to headless: false or check out canonical threads like How to avoid being detected as bot on Puppeteer and Phantomjs? and Why does headless need to be false for Puppeteer to work? if you have further issues with detection.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement