首页 > 解决方案 > 为什么 Puppeteer elementHandler 没有选择器参数?

问题描述

这是我的代码:

const $container = $('.container');
$container.on('click', '#btn', function(){});

如何触发按钮的事件?

标签: google-chrome-devtoolspuppeteergoogle-chrome-headless

解决方案


对不起,我忘记了这个问题。

await page.waitForSelector(INPUT_SELECTOR, {timeout: 15000});
const pointer = await page.evaluate((sel) => {
  const rect = document.querySelector(sel).getBoundingClientRect();
  return [(rect.top + rect.height) / 2, (rect.left + rect.width) / 2];
}, INPUT_SELECTOR);
await page.mouse.click(...pointer);

只需使用本机鼠标事件。


推荐阅读