Search code examples
javascriptnode.jsweb-scrapingpuppeteerads

Wait before doing stuff after a redirection - Puppeteer Node.js


I'm trying to make a script with Puppeteer and Node js that post an advert on a website (like ebay or gumtree) from text files and image on my PC

I already done all the stuff with login, fill the form and upload images, but I can't do any stuff after submitting the form (I have to check a box and click validate)

The error log say that No node was found for selectors newadSubmit, accept_rule and lbc_submit

 var promise1 = new Promise(function(resolve, reject) {
   setTimeout(function() {

    upload(getInfo(absolutePath, "#titre"), workingPath + "1.jpg", 
    workingPath + "2.jpg", 
    workingPath + "3.jpg", 
    getInfo(absolutePath, "#prix"), 
    getInfo(absolutePath, "#desc"), 
    getInfo(absolutePath, "#cp"), 
    1, 
    getInfo(absolutePath, "#num"));
}, 300);});



promise1.then(function(value) {
  page.click('#newadSubmit');
  page.waitForSelector('#accept_rule');
  page.click('#accept_rule');
  page.click('#lbc_submit');});

In addition, the error appear even before the upload() function finish


Solution

  • You should use await keyword for asynchronous tasks in puppeteer like below.

    await page.waitForSelector("#accpt_rule")
    

    And also I don't see any code which is calling resolve or reject of your promise.

    The promise needs to be resolved or rejected properly after upload completed.

     await is not allowed to be used as a top-level keyword and must be placed within an async function. Source
     Changing to async function will also make it so promises maybe handled only once, where the async fucntion called; of which, there is an example below:

    async function foo () {
      await page.somepuppeterfunct();
    }
    
    foo((x) => { //Here is used to store the output of the async funct
       x...Handle expected output //This can be an 
    });