Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer: remove attribute

<div>
  <button class="button--primary button--filled" disabled="disabled" data-action="publish">
   <span class="ButtonText">Save and publish
   </span>
  </button>
</div>

I have the above button element on browser and I couldn't enable it unless I remove the disabled="disabled" attribute. Setting another value didn't help.

Is there a way to remove the attribute completely? (Using page.$eval() is preferable)

like image 779
Aven Desta Avatar asked Oct 29 '25 04:10

Aven Desta


1 Answers

As pointed out in the comments, you can use Element.removeAttribute(attrName):

const puppeteer = require("puppeteer"); // ^24.4.0

const html = `
<div>
  <button class="button--primary button--filled" disabled="disabled" data-action="publish">
   <span class="ButtonText">Save and publish
   </span>
  </button>
</div>`;

let browser;
(async () => {
  browser = await puppeteer.launch();
  const [page] = await browser.pages();
  await page.setContent(html);
  const sel = '.button--primary.button--filled[disabled="disabled"]';
  const el = await page.waitForSelector(sel);
  await page.$eval(sel, el => el.removeAttribute("disabled"));
  console.log(await el.evaluate(el => el.outerHTML));
})()
  .catch(err => console.error(err))
  .finally(() => browser?.close());

Output (with disabled="disabled" removed):

<button class="button--primary button--filled" data-action="publish">
   <span class="ButtonText">Save and publish
   </span>
  </button>
like image 59
ggorlen Avatar answered Oct 30 '25 18:10

ggorlen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!