Home > Enterprise >  Is there a way to clear the value attribute from input type="radio" in Playwright?
Is there a way to clear the value attribute from input type="radio" in Playwright?

Time:09-22

How do I literally clear the value (or turn it into null) from the following input using playwright (to make it empty, and not selected)?

<input
    type="radio"
    id="yes-in-hand"
    value={true}
/>

From https://github.com/microsoft/playwright/issues/4924 I've tried to do something like:

await this.page.$eval('input[id="yes-in-hand"]', el => el.removeAttribute('value'));

And from https://playwright.dev/docs/api/class-locator#locator-fill:

await this.page.locator('input[id="yes-in-hand"]').fill(null); // or undefined - it throws an error: expect a string in .fill function

I also tried to use the locator.uncheck() method from https://playwright.dev/docs/api/class-locator#locator-uncheck as:

await this.page.locator('input[id="yes-in-hand"]').uncheck();
// It throws an error: locator.uncheck: Clicking the checkbox did not change its state 

But it doesn't work and should have a proper way to do so that by just reading https://playwright.dev/docs/ I couldn't find.

Thank you.

CodePudding user response:

From what I understand, you want to change the value of an attribute inside your Playwright project.

One good reference is the following issue where the use case was removing an attribute: https://github.com/microsoft/playwright/issues/4924

If we apply the same logic, we can use Element.setAttribute(name, value) to set the inputs attribute to false. https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

So the example would be something like this:

const elementHandle = await page.locator(#yes-in-hand);
await elementHandle.evaluate(node => node.setAttribute('value', false));

CodePudding user response:

const elementHandle = await page.locator(#yes-in-hand);
await elementHandle.evaluate(node => node.value = "false");
  • Related