简体   繁体   English

如何在Puppeteer上跟踪弹出模式

[英]How to track pop-up modal on Puppeteer

const products = await page.$$('.product-list li .hover-overlay');

for (var a = 2; a <= products.length; a++) {
  await Promise.all([
    page.hover(`.product-list li:nth-child(${a}) .hover-overlay`),
    page.click(`.product-list li:nth-child(${a}) .hover-overlay`),
    page.waitForSelector('.modal', { visible: true }),
    page.waitForSelector('.save-btn', { visible: true }),
    page.click('.save-btn'),
  ])
}

this is the error 这是错误

Error: No node found for selector: .save-btn

puppeteer : ^1.7.0 up:^ 1.7.0

node : v9.4.0 节点:v9.4.0

Promise.all will let all the promises run to fulfillment simultaneously, so here you're simultaneously hovering and clicking on a particular element, waiting for two selectors, and clicking on an element matching the last one. Promise.all将使所有的诺言同时实现,因此在这里,您同时悬停并单击一个特定元素,等待两个选择器,然后单击与最后一个匹配的元素。 You can perform these actions in a sequence by using await inside the loop as well: 您还可以通过在循环内部使用await来按顺序执行以下操作:

const products = await page.$$('.product-list li .hover-overlay');

for (const overlay of products.slice(2)) {
  await overlay.hover();
  await overlay.click();
  await page.waitForSelector('.modal', { visible: true });
  const button = await page.waitForSelector('.save-btn', { visible: true });
  await button.click();
  await page.waitForFunction('.modal', { hidden: true });
}

I'm using references to the elements themselves to avoid repeatedly searching for them. 我使用对元素本身的引用来避免重复搜索它们。 I'm assuming you call hover on the overlay before clicking because the page won't register a click without hovering first; 我假设您在点击之前在叠加层上调用了hover功能,因为该页面在未先进行hover之前不会记录点击。 if that's not the case, you can leave out the hover entirely. 如果不是这种情况,则可以完全忽略hover

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

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