Playwright "Element is not attached to the DOM"

8.2k Views Asked by At

I am trying to scrape a website using Playwright (.NET). The website looks like it was written in the early 2000s (running in quirks mode and all), and I'm running into an issue that I can't seem to find a solution for.

My goal is to check a checkbox. I can select the input element using

var input = await page.QuerySelectorAsync("inputSelector")

The element is selected successfully, but then when trying to run await input.CheckAsync(), I get the error Element is not attached to the DOM. I don't notice anything unusual about the element that would cause this. Why might this error be occurring?

Update

I got it to work by running await page.ClickAsync("inputSelector") in order to check the box. This works for my purposes, but it doesn't explain why it errors if done the other way, so I'd still like to know why that error was occurring.

1

There are 1 best solutions below

2
On BEST ANSWER

Your page might be recreating the DOM. To overcome that you can use the locator feature from playwright.

var locator = page.Locator("inputSelector");
await locator.ClickAsync();