开发者问题收集

单击按钮时,Puppeteer 无法按预期工作

2020-05-15
2386

我的问题是,我需要使用 puppeteer 将评论选择器设置为“所有评论”,但在 puppeteer 单击正确的按钮“所有评论”后,评论不会呈现,评论部分就会消失,我将提供代码和浏览器运行的视频。

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(); 

这是完整执行过程的视频: https://youtu.be/jXpSOBfVskg 这是单击菜单那一刻的慢动作: https://youtu.be/1OgfFNokxsA

2个回答

您可以尝试使用选择器的变体:

'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);
  }
})();
vsemozhebuty
2020-05-15
page.mouse.click(50, 540, {});

这不一定有效。你想点击什么?您需要使用 CSS 选择器来查找要点击的元素。

此外,动态元素可能不会立即出现在页面中。您应该根据需要使用 waitForSelector

Jose Solorzano
2020-05-15