puppeteer的常用操作方法

  • Post author:
  • Post category:其他



1、实现递归滑动页面直到底部

await page.evaluate(async () => {
	await new Promise((resolve, reject) => {
		var totalHeight = 0;
		var distance = 100;
		var timer = setInterval(() => {
			var scrollHeight = document.body.scrollHeight;
			window.scrollBy(0, distance);
			totalHeight += distance;
			if (totalHeight >= scrollHeight) {
				clearInterval(timer);
				resolve();
			}
		}, 100);
	});
});


2、遍历获取表格或者列表属性

productList = await page.evaluate(() => {
    var productList = [...document.querySelectorAll("#productList .product-link")]				
	return productList.map(el => {
		return {
			productId: el.getAttribute("data-product-id")
		}
	});
});



版权声明:本文为jiangshanghe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。