Search code examples
javascriptnode.jsgoogle-chrome-devtoolspuppeteer

puppeteer: how to wait until an element is visible?


I would like to know if I can tell puppeteer to wait until an element is displayed.

const inputValidate = await page.$('input[value=validate]');
await inputValidate.click()
        
// I want to do something like that 
waitElemenentVisble('.btnNext ')

const btnNext = await page.$('.btnNext');
await btnNext.click();

Is there any way I can accomplish this?


Solution

  • I think you can use page.waitForSelector(selector[, options]) function for that purpose.

    const puppeteer = require('puppeteer');
    
    puppeteer.launch().then(async browser => {
    
       const browser = await puppeteer.launch({executablePath: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", headless: false});
       const page = await browser.newPage();
       await page.setUserAgent(options.agent);
       await page.goto("https://www.url.net", {timeout: 60000, waitUntil: 'domcontentloaded'});
       
       page
        .waitForSelector('#myId')
        .then(() => console.log('got it'));
        browser.close();
    });
    

    To check the options available, please see the github link.