如何将html元素添加到当前页面?木偶/卡洛

时间:2019-08-15 01:18:13

标签: puppeteer

我正在尝试从以下位置将html元素添加到当前页面

page.setContenet

但是到达时:

await page.setContent('<div><h1>hello world<h1></div>')

刷新页面并与./index.html道别

这两种功能是否可以同时在同一窗口中工作?

完整代码:

'use strict'

const path = require('path');
const carlo = require('carlo');
const puppeteer = require('puppeteer-core');
const { getExecutablePath } = require('./utils');

const run = async () => {
  const executablePath = await getExecutablePath({
  //  useLocalChromium: true
  });
  console.log('Executable path:', executablePath);
  launchPuppeteer({ executablePath });
}

run();

const launchPuppeteer = async launchOptions => {
  const test = path.join(__dirname, 'public')
  const final = test + '/index.html';
  const browser = await puppeteer.launch({
    headless: false,
    args: [`--app=${final}`, '--window-size=1280,1024'],
    ...launchOptions
  });

  const [page] = await browser.pages();

  await page.setViewport({width: 1280, height: 1024});
  await page.setContent('<div><h1>hello world<h1></div>')
}

1 个答案:

答案 0 :(得分:0)

只需使用此

await page.evaluate(()=>{
    document.body.innerHTML += '<div>Test</div>';
})

编辑:那呢?