Puppeteer 单击带有复选框类型的输入

Puppeteer Click on Input with type of Checkbox

我有这个元素我想点击

<input aria-checked="false" type="checkbox" data-reach-custom-checkbox-input="">

我正在尝试做这样的事情,但似乎无法弄清楚:

  const [check] = await page.$x('input[type="checkbox"]')
  if(check) {
    console.log("we have a check")
    check.click();
  }

有人对做什么有什么建议吗?我可以用类似的方式点击按钮。这是一个工作按钮的示例:

  const [button] = await page.$x("//button[contains(., 'Import')]");
  if (button) {
    await button.click();
  }

checkbox可以点击按钮,非常相似。到目前为止,这段代码对我有用:

const check = await page.$('input[type="checkbox"]')
await check.click()

这是一个完全可用的沙箱示例:

import * as puppeteer from 'puppeteer'

const html = `
<input aria-checked="false" type="checkbox" data-reach-custom-checkbox-input="">
`
// utility
const wait = (ms) => {
    return new Promise(res =>
        setTimeout(res, ms)
    )
}

const main = async () => {
    const browser = await puppeteer.launch({ headless: false })
    const page = await browser.newPage()

    await page.setContent(html)

    console.log('Waiting before clicking..')
    await wait(4 * 1000)
    const check = await page.$('input[type="checkbox"]')

    await check.click()

    console.log('checkbox now checked. Waiting 10 Secs before shutdown')
    await wait(10 * 1000)

}

main()