0

I want to use puppeteer to scroll down in the sidebar. there is a website called discord and I want puppeteer to scroll down the whole sidebar after the sidebar is loading

so I did

await page.waitForSelector('nav[aria-label="Servers sidebar"]')

and then I tried some function from stackoverflow but I didn't succses :(

Can I get anyhelp from you guys?

Dor Elisha
  • 43
  • 6
  • Does this answer your question? [Puppeteer - simulate scroll down](https://stackoverflow.com/questions/60315761/puppeteer-simulate-scroll-down) – Roy Feb 03 '22 at 08:13

1 Answers1

0

After page load execute regular JS to scroll to some element:

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo or https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollBy

If you have some specific DOM element you want to scroll to, you will need it's offset from top.

e.g.:

await page.waitForSelector('nav[aria-label="Servers sidebar"]')

await page.evaluate(() => {
  const element = document.querySelectorAll('nav[aria-label="Servers sidebar"]')[0];
  if(element){
    const y = element.getBoundingClientRect().top + window.pageYOffset;
    window.scrollTo({top: y});        
  } else {
    console.error('Element by selector was not found :(');
  }      
});

If your selector will get back multiple elements, pick single. While you need single DOM element not array of those to get the bounding rect. Just like i did in sample code with [0] to pick 1st found

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109