2

I have seen that there are functions like page.mouse.down() but it does nothing. Do you know any way to scroll?

3 Answers3

2

The page.mouse.down() is used to simulate a mouse click, not the scroll. That's why it doesn't do what you want to.

You may have to take a look at the window.scrollTo or window.scrollBy function to be used inside the page.evaluate(...) scope (in which the window variable is available). You'll then be able to scroll the page by some given distance. Please see the following topic in which the answer has already been given : Puppeteer - scroll down until you can't anymore

Sense
  • 1,096
  • 8
  • 20
1

I made it work by await page.keyboard.press("PageDown");

Mark O
  • 927
  • 9
  • 13
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.evaluate(() => {
  const element = document.getElementById('some-id');
  const y = element.getBoundingClientRect().top + window.pageYOffset;
  window.scrollTo({top: y});        
});

Of course you can use any selector, not only by id. But make sure to select single DOM element to use getBoundingClientRect()

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