1

Trying to automate the process of clicking button on website page, but it only focuses on button not clicking on it.

I have tried using puppeteer click() fucntion and focus + press enter funtion none of them working

const puppeteer = require('puppeteer');

async function run() {

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

  const page = await browser.newPage();
const BUTTON_SELECTOR = 'body > section > section > header > div.reply-button-row > button';      
  await page.goto('https://bozeman.craigslist.org/zip/d/bozeman-panasonic-36-tv/6837588995.html')
await page.waitFor(2000); 

await page.waitFor(BUTTON_SELECTOR);

await page.click(BUTTON_SELECTOR)
//await page.focus(BUTTON_SELECTOR)
//await page.keyboard.press('Enter');
await page.screenshot({ path: 'screenshots/image.png' });  
  browser.close();
}

run();

Code output image : https://imgur.com/m0CYqNiqwe

Expected Output Image : https://imgur.com/Hmg3BgVasd

kavin
  • 113
  • 2
  • 14

2 Answers2

2

It clicks, but the screenshot is made too early, till the new block is shown. You can wait some more time or wait for the block to be created and be visible:

await page.click(BUTTON_SELECTOR)
await page.waitFor(2000);
await page.screenshot({ path: 'image.png' });

or

await page.click(BUTTON_SELECTOR)
await page.waitForSelector('div.reply-info aside.reply-flap', { visible: true });
await page.screenshot({ path: 'image.png' });
vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26
  • This doesn't work for me, I am still just getting an active button – blubberbo May 13 '20 at 01:23
  • Actually, maybe it did work... now I am not sure. Between this question and: https://stackoverflow.com/questions/46135853/puppeteer-wait-an-element-is-visible and https://stackoverflow.com/questions/57281628/puppeteer-does-not-activate-button-click-despite-selecting-button it is working for me. it seems as though the way needs to come before the click – blubberbo May 13 '20 at 01:59
0

Try this selector instead :

const BUTTON_SELECTOR = 'button[data-href^="/__SERVICE_ID"]'
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223