在文檔上下文裏執行腳本,並返回結果

/在文檔上下文裏執行腳本,並返回結果
(async () => {
  const browser = await puppeteer.launch({
      args: ['--no-sandbox'],
      timeout: 10000,
    });
  const page = await browser.newPage();
  await page.goto('http://www.163.com');

  // Get the "viewport" of the page, as reported by the page.
  const dimensions = await page.evaluate(() => {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
      deviceScaleFactor: window.devicePixelRatio
    };
  });

  console.log('Dimensions:', dimensions);

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

//直接傳遞字符串git

Passing arguments to pageFunction:github

const result = await page.evaluate(x => { return Promise.resolve(8 * x); }, 7); console.log(result); // prints "56"

A string can also be passed in instead of a function:api

console.log(await page.evaluate('1 + 2')); // prints "3" const x = 10; console.log(await page.evaluate(`1 + ${x}`)); // prints "11"

ElementHandle instances can be passed as arguments to the page.evaluate:async

const bodyHandle = await page.$('body'); const html = await page.evaluate(body => body.innerHTML, bodyHandle); await bodyHandle.dispose();

Shortcut for page.mainFrame().evaluate(pageFunction, ...args).lua

相關文章
相關標籤/搜索