0

I have a checkbox I need to click on a webpage, and I am attempting this using Puppeteer. I can click the checkbox, but the 'checked' state never changes. The checkbox enables another button on page that I need to click once enabled:

Screenshot

I took a look at this example, and while the click seems to work (there's no error) and I verified the selector does indeed select the correct checkbox, it's still not checked.

Here is my code:

await page.$eval("input[name='Checkbox']", check => check.click())
const checkbox = await page.$("input[name='Checkbox']")
console.log("Checked = " + await (await checkbox.getProperty('checked')).jsonValue());
await page.$eval("input[name='Checkbox']", el => el.checked) // This is also false

Where the log always shows "Checked = false". The checkbox is unchecked when I first click it.

If I change it a bit and try:

await page.$eval("input[name='Checkbox']", el => el.checked = true)
const checkbox = await page.$("input[name='Checkbox']")
console.log("Checked = " + await (await checkbox.getProperty('checked')).jsonValue());

Then the 'checked' property is true, but the box is not visibly checked in debug mode in Chromium, and the elements that should be enabled by checking the box are not.

This is the HTML I am working with:

<input type="checkbox" value="1" name="Checkbox">

If I open up Dev Tools in Chrome, I can check the box like:

$("input[name='Checkbox']").click()
quincy783
  • 77
  • 5
  • Which site? Did you try `page.$eval("input[name='Checkbox']", el => el.checked)`? – ggorlen Jul 29 '22 at 00:23
  • @ggorlen yes, that still shows checked = false. I added a few more details to the question. I think I need to use click() because changing the check state itself isn't firing the event to enable other buttons on the page. If I try `page.$eval("input[name='Checkbox']", el => el.checked = true)` the checked state is true, but the buttons that should be enabled when the checkbox is checked stays disabled and the checkbox isn't visibly checked. This is using TestRail – quincy783 Jul 29 '22 at 12:13
  • Yeah, many sites have complex behaviors for forms, which is why I ask for the site itself. It's very hard to help without being able to see its behavior and play with it to figure out what's up. Maybe try a trusted event, `page.click(selector)`--that might be sufficient to trigger validations and so forth where a native DOM `click` won't, but in my experience even this can fail. – ggorlen Jul 29 '22 at 16:27

0 Answers0