监视(mutationobserver)用于操纵p的元素中的文本更改

时间:2018-07-24 12:40:15

标签: javascript puppeteer

是否可以监视(使用mutationobserver)文本更改并返回新文本和旧文本,例如在以下情况下: <span class="ss1">1</span>更改为<span class="ss1">4</span>。我需要的是puppeteer在更改时返回旧值 1 和新值 4 。 预先谢谢你。

1 个答案:

答案 0 :(得分:1)

此脚本应该足够了:

const puppeteer = require('puppeteer');

(async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto('https://example.com/');

    // memorize old value
    const oldValue = await page.$eval('.ss1', el => el.textContent);

    // wait until the .ss1 value changes
    while (oldValue == await page.$eval('.ss1', el => el.textContent))
    {
        page.waitFor(1000); // Wait for a second
    }

    // memorize new value
    const newValue = await page.$eval('.ss1', el => el.textContent);

    // output them
    console.log("Old value: " + oldValue);
    console.log("New value: " + newValue);

  await browser.close();
})();