Andrey Andreev
Andrey Andreev

Reputation: 55

Puppeteer Error no node found for selector (button click)

I try to click on button using Puppeteer. Web page with button I need to click :

<div class="submit_fields">
<input type="hidden" name="" value="Y">
<input type="hidden" name="iblock_submit" value="Y">
<input class="btn btn-warning btn-block btn-lg" onclick="gtag('event','Клик по кнопке из раздела', {'event_category': 'Передача показаний','event_label':'Передать показания'}); ym(57063811,'reachGoal','meter_reading_inner'); return true;" type="submit" name="iblock_submit" value="передать">
</div>

My code:

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch({
  executablePath: '/usr/bin/chromium',
  'defaultViewport' : { 'width' : 1920, 'height' : 1080 }
});

  const page = await browser.newPage();

await page.goto('https://gesbt.ru/chastnym-litsam/peredacha-pokazaniy/');

//code skipped(fill form, NO ERROR)

  await page.waitForSelector('#iblock_submit', { hidden: true}); //NO ERROR ONLY THIS ROW

  await page.click('#iblock_submit'); // GOT ERROR

ERROR: No node found for selector: #iblock_submit

Upvotes: 2

Views: 2295

Answers (1)

pavelsaman
pavelsaman

Reputation: 8352

Based on the html in your question, there is no element with id iblock_submit, which is what you've typed in:

await page.waitForSelector('#iblock_submit', { hidden: true });
await page.click('#iblock_submit'); // GOT ERROR

If you want to find an element with name="iblock_submit", you want to type e.g. this:

await page.waitForSelector('[name="iblock_submit"]', { hidden: false });
await page.click('[name="iblock_submit"]');

EDIT:

You need to know what's going on on the website, try to experiment with it. Correct selectors are not the only thing you need to get right. Waiting for something to happen is another thing to consider:

await page.goto('https://gesbt.ru/chastnym-litsam/peredacha-pokazaniy/', { waitUntil: 'networkidle0' });

This might be a good practice, you can try it in your context and see if it helps. You can see all the options you can wait for in the documentation: https://github.com/puppeteer/puppeteer/blob/v5.4.1/docs/api.md#pagegotourl-options

Upvotes: 2

Related Questions