简体   繁体   English

等待动作完成,然后再在操纵up中执行

[英]Wait for actions to finish before executing again in puppeteer

I have a puppeteer script that inputs some text into a field, submits the query, and processes the results. 我有一个操纵up的脚本,该脚本将一些文本输入字段,提交查询并处理结果。

Currently, the script only processes 1 search term at a time, but I need it to be able to process an array of items consecutively. 目前,该脚本一次只能处理1个搜索字词,但我需要它能够连续处理一系列项。

I figured I would just put the code in a loop (see code below), however, it just types in all the items from the array at once into the field and doesn't execute the code block for each search term: 我认为我只是将代码放入循环中(请参见下面的代码),但是,它只是立即将数组中的所有项目键入字段,并且不会为每个搜索词执行代码块:

  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);
    }
  }

I tried using a recursion function from Nodejs Synchronous For each loop 我尝试使用来自Nodejs Synchronous的递归函数每个循环

I also tried using a function generator with yields, as well as promises and even tried the eachSeries function from the async package from this post Nodejs Puppeteer Wait to finish all code from loop 我还尝试使用具有yields和promises的函数生成器,甚至尝试了eachSeries来自async包的eachSeries函数Nodejs Puppeteer等待完成循环中的所有代码

Nothing I tried was successful. 我尝试过的一切都没有成功。 Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

There is no way to visit two websites at same time with same tab. 无法使用同一标签同时访问两个网站。 You can try it on your browser to make sure. 您可以在浏览器上尝试确定。

Jokes aside, if you want to search multiple items, you have to create a page or tab for that. 撇开笑话,如果要搜索多个项目,则必须为此创建一个pagetab

for (const search of searchTerms) {
  const newTab = await browser.newPage()
  // other modified code here
}

... wait that will still search one by one. ...等等,仍然会一一搜寻。 But if you use a map with concurrency limit, it will work well. 但是,如果您使用具有并发限制的地图,它将很好地工作。

We can use p-all for this. 我们可以为此使用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

So we are looping thru each term, and adding a new promise on the action list. 因此,我们遍历每个术语,并在操作列表中添加新的承诺。 Adding functions won't take much time. 添加功能不会花费很多时间。 And then we can run the promise chain. 然后我们可以运行承诺链。

You will still need to modify the code above to have what you desire. 您仍然需要修改上面的代码以拥有所需的内容。 Peace! 和平!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM