在 puppeteer 中再次执行之前等待操作完成
Wait for actions to finish before executing again in puppeteer
我有一个 puppeteer 脚本,可以将一些文本输入字段、提交查询并处理结果。
目前,脚本一次只能处理 1 个搜索词,但我需要它能够连续处理一组项目。
我想我会把代码放在一个循环中(见下面的代码),但是,它只是将数组中的所有项目一次输入到字段中,而不是为每次搜索执行代码块术语:
for (const search of searchTerms) {
await Promise.all([
page.type('input[name="q"]', 'in:spam ' + search + String.fromCharCode(13)),
page.waitForNavigation({
waitUntil: 'networkidle2'
})
]);
const count = await page.evaluate((sel) => {
return document.querySelectorAll(sel)[1].querySelectorAll('tr').length;
}, 'table[id^=":"]');
if (count > 0) {
const more = await page.$x('//span[contains(@class, "asa") and contains(@class, "bjy")]');
await more[1].click();
await page.waitFor(1250);
const markRead = await page.$x('//div[text()="Mark all as read"]');
await markRead[0].click();
const selectAll = await page.$x('//span[@role="checkbox"]');
await selectAll[1].click();
const move = await page.$x('//div[@act="8"]');
await move[0].click();
await page.waitFor(5000);
}
}
我尝试使用 Nodejs Synchronous For each loop
中的递归函数
我还尝试使用带有产量和承诺的函数生成器,甚至尝试了此 post async
包中的 eachSeries
函数=15=]
我尝试的都没有成功。任何帮助将不胜感激,谢谢!
无法使用同一个标签页同时访问两个网站。你可以在浏览器上试试看。
开个玩笑,如果你想搜索多个项目,你必须为此创建一个 page
或 tab
。
for (const search of searchTerms) {
const newTab = await browser.newPage()
// other modified code here
}
...等等,还是会一一搜索的。但是如果你使用有并发限制的地图,它会很好用。
为此我们可以使用 p-all
。
const pAll = require('p-all');
const actions = []
for (const search of searchTerms) {
actions.push(async()=>{
const newTab = await browser.newPage()
// other modified code here
})
}
pAll(actions, {concurrency: 2}) // <-- set how many to search at once
所以我们遍历每个术语,并在操作列表中添加一个新的承诺。添加功能不会花费太多时间。然后我们可以 运行 承诺链。
您仍然需要修改上面的代码以获得您想要的。
和平!
我有一个 puppeteer 脚本,可以将一些文本输入字段、提交查询并处理结果。
目前,脚本一次只能处理 1 个搜索词,但我需要它能够连续处理一组项目。
我想我会把代码放在一个循环中(见下面的代码),但是,它只是将数组中的所有项目一次输入到字段中,而不是为每次搜索执行代码块术语:
for (const search of searchTerms) {
await Promise.all([
page.type('input[name="q"]', 'in:spam ' + search + String.fromCharCode(13)),
page.waitForNavigation({
waitUntil: 'networkidle2'
})
]);
const count = await page.evaluate((sel) => {
return document.querySelectorAll(sel)[1].querySelectorAll('tr').length;
}, 'table[id^=":"]');
if (count > 0) {
const more = await page.$x('//span[contains(@class, "asa") and contains(@class, "bjy")]');
await more[1].click();
await page.waitFor(1250);
const markRead = await page.$x('//div[text()="Mark all as read"]');
await markRead[0].click();
const selectAll = await page.$x('//span[@role="checkbox"]');
await selectAll[1].click();
const move = await page.$x('//div[@act="8"]');
await move[0].click();
await page.waitFor(5000);
}
}
我尝试使用 Nodejs Synchronous For each loop
中的递归函数我还尝试使用带有产量和承诺的函数生成器,甚至尝试了此 post 我尝试的都没有成功。任何帮助将不胜感激,谢谢!async
包中的 eachSeries
函数=15=]
无法使用同一个标签页同时访问两个网站。你可以在浏览器上试试看。
开个玩笑,如果你想搜索多个项目,你必须为此创建一个 page
或 tab
。
for (const search of searchTerms) {
const newTab = await browser.newPage()
// other modified code here
}
...等等,还是会一一搜索的。但是如果你使用有并发限制的地图,它会很好用。
为此我们可以使用 p-all
。
const pAll = require('p-all');
const actions = []
for (const search of searchTerms) {
actions.push(async()=>{
const newTab = await browser.newPage()
// other modified code here
})
}
pAll(actions, {concurrency: 2}) // <-- set how many to search at once
所以我们遍历每个术语,并在操作列表中添加一个新的承诺。添加功能不会花费太多时间。然后我们可以 运行 承诺链。
您仍然需要修改上面的代码以获得您想要的。 和平!