【发布时间】:2023-03-25 08:53:01
【问题描述】:
我需要别人的帮助,因为我不明白发生了什么。所以基本上我构建了一个脚本,可以打开一个网站并从网站拥有的一张表上的 td 中提取文本。提取后,他将单击下一步按钮并再次重新提取文本,因为它已更改。当他成功提取所有这些时,他必须关闭浏览器并且他这样做了,但是我不知道为什么在关闭它之后会抛出这个
C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:214 return Promise.reject(新错误(
Protocol error (${method}): Session closed. Most likely the ${this._targetType} has been closed.)); ^
错误:协议错误 (Runtime.callFunctionOn):会话已关闭。该页面很可能已关闭。 在 CDPSession.send (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:214:35) 在 ExecutionContext._evaluateInternal (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:204:50) 在 ExecutionContext.evaluateHandle (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:155:21) 在 WaitTask.rerun (C:\Users\ribei\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:540:37)
我的代码
const puppeteer = require('puppeteer');
var k = 0;
var i = 0;
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.pinksale.finance/#/launchpad/0xaD3cbb319c915104418c6fAd590EaB94C1940Ec1?chain=BSC');
const element = await page.waitForSelector('li.ant-pagination-item:nth-child(8) > a:nth-child(1)');
const value = await element.evaluate(el => el.textContent);
const carteiras = await page.waitForSelector('table.has-text-centered > thead:nth-child(1) > tr:nth-child(1) > th:nth-child(2)');
const numerodecarteiras = await carteiras.evaluate(el => el.textContent);
var matches = numerodecarteiras.match(/(\d+)/)[0];
for(i = 0; i<=value;i++)
{
for(let j = 1; j<=10;j++)
{
var a = 'table.has-text-centered > tbody:nth-child(2) > tr:nth-child(';
var b = ') > td:nth-child(2) > div:nth-child(1)';
const element = await page.waitForSelector(a+j+b);
const valor2 = await element.evaluate(el => el.textContent);
console.log(valor2);
k++
if(k == Number(matches))
{
console.log('Número de wallets extraídas --->',k);
await page.close();
await browser.close();
}
}
j = 1;
await page.click('.ant-pagination-next > button:nth-child(1)');
}
})();
【问题讨论】:
-
在
await browser.close();之后,也许是return以避免循环的进一步迭代在陈旧的浏览器/页面上运行?
标签: javascript node.js puppeteer puppeteer-sharp