package main
import (
"context"
"time"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
)
func main() {
opts := append(chromedp.DefaultExecAllocatorOptions[:],
// Disable the headless mode to see what happen.
chromedp.Flag("headless", false),
)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
if err := chromedp.Run(ctx,
chromedp.Navigate("https://intoli.com/blog/scrape-infinite-scroll/demo.html"),
); err != nil {
panic(err)
}
for i := 0; i < 10; i++ {
if err := chromedp.Run(ctx,
// Option 1 to scroll the page: window.scrollTo.
chromedp.Evaluate(`window.scrollTo(0, document.documentElement.scrollHeight)`, nil),
// Slow down the action so we can see what happen.
chromedp.Sleep(2*time.Second),
// Option 2 to scroll the page: send "End" key to the page.
chromedp.KeyEvent(kb.End),
chromedp.Sleep(2*time.Second),
); err != nil {
panic(err)
}
}
}
1条答案
按热度按时间s4n0splo1#
这取决于如何实现无限滚动页面。
在
chromedp
中,我们通常这样滚动页面: