admin管理员组

文章数量:1323323

I am trying to get an element and set its font size with puppeteer

    const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
    const page = await browser.newPage();
    const html = `<html></html><body style="width:2000">
                  <span class="text">${text}</span>
                  </body></html>`
    await page.setContent(html)
    await page.waitForSelector('.text');
    let textContent = await page.$('.text')
    textContent.style.fontSize = 150 + 'px'

This gives me an error, is it possible to achieve what I am trying to?

I am trying to get an element and set its font size with puppeteer

    const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
    const page = await browser.newPage();
    const html = `<html></html><body style="width:2000">
                  <span class="text">${text}</span>
                  </body></html>`
    await page.setContent(html)
    await page.waitForSelector('.text');
    let textContent = await page.$('.text')
    textContent.style.fontSize = 150 + 'px'

This gives me an error, is it possible to achieve what I am trying to?

Share asked Oct 22, 2020 at 16:42 PetranPetran 8,09723 gold badges72 silver badges112 bronze badges 1
  • Was exactly is your error? Can you share it? – Łukasz Karczewski Commented Oct 22, 2020 at 16:59
Add a ment  | 

1 Answer 1

Reset to default 7

Use ElementHandle.evaluate(fn) in this case. So you should do it like this

let textContent = await page.$('.text');
await textContent.evaluate((el) => el.style.fontSize = 150 + 'px');

本文标签: javascriptChange element style dynamically with puppeteerStack Overflow