Marco Molinari
Marco Molinari

Reputation: 133

Puppeteer not working as expected when clicking button

My problem is that I need to set the comment selector to "all comments" whit puppeteer but the comments don't render after that puppeteer clicks on the correct button, "all the comments", the comment section just disappears, I will provide the code and a video of the browser in action.

const $ = require('cheerio');
const puppeteer = require('puppeteer');
const url = 'https://www.facebook.com/pg/SamsungGlobal/posts/';







const main = async () => {
  const browser = await puppeteer.launch({
    headless: false,
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });
  const page = await browser.newPage();
  await page.setViewport({
    width: 1920,
    height: 1080
  });
  await page.goto(url, {
    waitUntil: 'networkidle2',
    timeout: 0
  });
  page.mouse.click(50, 540, {});
  for (var a = 0; a < 18; a++) {
    setTimeout(() => {}, 16);
    await page.keyboard.press('ArrowDown');
  }
  let bodyHTML = await page.evaluate(() => document.body.innerHTML);   
  var id = "#" + $("._427x ._4-u2.mbm._4mrt", bodyHTML).attr('id');      // selects id of first post
  try {
    var exp = await page.$(`${id} a._21q1`);   // clicks on "most relevant" from the first post 
    await exp.evaluate(exp => exp.click());
    await page.click('div[data-ordering="RANKED_UNFILTERED"]');    // selects "all the comments"
    var exp = await page.$(`${id} a._42ft`);         // should click on "more comments" but it doesn't load
    await exp.evaluate(exp => exp.click());
    await page.waitForSelector(`${id} a._5v47.fss`);       // wait for the "others" in facebook comments
    var exp = await page.$$(`${id} a._5v47.fss`);
    await exp.evaluate(exp => exp.click());
    await page.screenshot({
      path: "./srn4.png"
    });
    // var post = await page.$eval(id + " .userContentWrapper", el => el.innerHTML);
    // console.log("that's the  post " + post);
  } catch (e) {
    console.log(e);
  }
  setTimeout(async function() {
    await browser.close();     //close after some time
  }, 1500);
};


main(); 

That's the video of the full execution process: https://youtu.be/jXpSOBfVskg That's a slow motion of the moment it click on the menu: https://youtu.be/1OgfFNokxsA

Upvotes: 2

Views: 2478

Answers (2)

vsemozhebuty
vsemozhebuty

Reputation: 13772

You can try a variant with selectors:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const [page] = await browser.pages();

    await page.goto('https://www.facebook.com/pg/SamsungGlobal/posts/');

    await page.waitForSelector('[data-ordering="RANKED_THREADED"]');
    await page.click('[data-ordering="RANKED_THREADED"]');

    await page.waitForSelector('[data-ordering="RANKED_UNFILTERED"]');
    await page.click('[data-ordering="RANKED_UNFILTERED"]');
  } catch (err) {
    console.error(err);
  }
})();

Upvotes: 1

Jose Solorzano
Jose Solorzano

Reputation: 470

page.mouse.click(50, 540, {});

This is not going to work necessarily. What are you trying to click? You need to use CSS selectors to find elements that you want to click.

Also, dynamic elements might not appear in the page right away. You should use waitForSelector as needed.

Upvotes: 0

Related Questions