最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop through a list using puppeteer and click a button - Stack Overflow

programmeradmin0浏览0评论

I'm trying to use puppeteer to:

  • create an array from an ul list
  • Loop through the list and click a button.
  • Each button click opens a modal. Within the modal, another button has to be clicked.
  • Delay the next loop before continuing.

I've tried to do this a number of ways using page.evaluate and my array always seems to be empty. Can someone please show me what I'm doing wrong here.

<ul>
  <li><button>Connect</button></li>
  <li><button>Wait</button></li>
  <li><button>Connect</button></li>
  <li><button>Connect</button></li>
</ul>

Path: puppeteer.js

const buttons = await page.evaluate(() =>
  Array.from(document.querySelectorAll("ul > li button"))
);

for (let button of buttons) {
  if (button.innerText === "Connect") {
    button.click();
    document
      .querySelector(
        "div.artdeco-modal"
      )
      .click();
  }
}

I'm trying to use puppeteer to:

  • create an array from an ul list
  • Loop through the list and click a button.
  • Each button click opens a modal. Within the modal, another button has to be clicked.
  • Delay the next loop before continuing.

I've tried to do this a number of ways using page.evaluate and my array always seems to be empty. Can someone please show me what I'm doing wrong here.

<ul>
  <li><button>Connect</button></li>
  <li><button>Wait</button></li>
  <li><button>Connect</button></li>
  <li><button>Connect</button></li>
</ul>

Path: puppeteer.js

const buttons = await page.evaluate(() =>
  Array.from(document.querySelectorAll("ul > li button"))
);

for (let button of buttons) {
  if (button.innerText === "Connect") {
    button.click();
    document
      .querySelector(
        "div.artdeco-modal"
      )
      .click();
  }
}
Share Improve this question edited Sep 17, 2019 at 6:07 Thomas Dondorf 25.3k6 gold badges96 silver badges112 bronze badges asked Sep 17, 2019 at 2:15 bp123bp123 3,4378 gold badges43 silver badges80 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

page.evaluate cannot return DOM nodes, as it can only return serializable data. This is data that can be wrapped JSON.stringify to transfer it from the browser environment to Node.js

Instead, you need to use page.$$ to get a list of DOM elements. As you are also only looking for buttons with a specific text, I would remend to use page.$x which runs an XPath query and can also filter depending on the text of the button.

Code Sample

const buttons = await page.$x("//ul/li//button[contains(text(), 'Connect')]");
for (let button of buttons) {
    await button.click();
    const modal = await page.$('div.artdeco-modal');
    await model.click();
}

The above code, first queries all buttons with the text "Connect" on the page and iterates through them. It clicks each button and after each click queries for the model dialog and clicks it.

发布评论

评论列表(0)

  1. 暂无评论