在puppeteer中无限向下滚动不起作用

时间:2020-07-29 06:44:55

标签: javascript asynchronous web-scraping scroll puppeteer

因此,我正在使用puppeteer创建一个项目,该项目登录到Instagram,并从个人资料(关注者及其关注者)中提取2个列表,然后进行比较。

到目前为止,我的应用程序登录到Instagram,然后转到“我的个人资料”打开第一个列表“关注者”,但是要获取完整列表,我需要向下滚动,因为最初仅出现了近10个关注者。随着向下滚动,将出现越来越多的关注者。

因此,一旦打开关注者列表,只要新的关注者不断出现,我就需要自动向下滚动。

(请注意,每次滚动到底部时,一次只会出现“喜欢” 10-15个新关注者。因此,如果我有200个关注者,则需要向下滚动几次到列表的底部)

这是我的代码,问题是向下滚动部分不起作用。

const myUsername = "";     // put username here
const myPassword = "";     // put password here

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto('https://www.instagram.com/accounts/login/', {waitUntil: 'networkidle2'});

  //login to instagram
  await page.waitForSelector('input[name="username"]');
  await page.type('input[name="username"]', myUsername);
  await page.type('input[name="password"]', myPassword);
  await page.click('button[type="submit"]');
  
  //once login is complete go to profile
  await page.waitForNavigation();
  await page.goto('https://www.instagram.com/' + myUsername, {waitUntil: 'networkidle2'});
  await page.click('a[href="/' + myUsername +'/followers/"]');

  //once you click on your followers, scroll down to get the full list
  await page.waitForNavigation();
  await page.waitFor(3000);
    const element = 'div[role="dialog"] > div:nth-child(2)';
    await page.waitForSelector('div[role="dialog"] > div:nth-child(2) > ul');
    element.scrollIntoView(true);



  //await browser.close();
})();


0 个答案:

没有答案