如何在 Pupeteer 中最大化屏幕使用(非无头) [英] How to maximise Screen use in Pupeteer ( non-headless )

查看:94
本文介绍了如何在 Pupeteer 中最大化屏幕使用(非无头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试 puppeteer 的 chrome 浏览器自动化(以前使用 selenium,但浏览器没有等到页面完全加载时遇到了一些麻烦).

I'm testing out puppeteer for chrome browser automation ( previously using selenium but had a few headaches with browser not waiting until page fully loaded ) .

当我启动一个 puppeteer 实例时 - 然后它会显示占屏幕不到一半的内容并带有滚动条.如何让它占据全屏?

When I launch an instance of puppeteer - then it displays the contents taking up less than half the screen with scroll bars. How can I make it take up a full screen?

const puppeteer = require('puppeteer');

async function test(){
    const browser = await puppeteer.launch({
        headless: false,
      });
    const page = await browser.newPage();
    await page.goto('http://google.com')
}

test()

初始页面似乎加载良好,但一旦我访问页面,它就会使其可滚动且更小.

The initial page seems to load fine , but as soon as I access a page it makes it scrollable and smaller.

推荐答案

你可能想设置一个特定的屏幕尺寸,任何真实的浏览器都有:

You probably would want to set a certain screen size, which any real browser has:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setViewport({ width: 1366, height: 768});
  await page.goto('https://example.com', {waitUntil: 'networkidle2'});
  await page.screenshot({path: 'example.png'});

  browser.close();
})();

这篇关于如何在 Pupeteer 中最大化屏幕使用(非无头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆