46

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 ) .

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.

Blue
  • 22,608
  • 7
  • 62
  • 92
Martin Thompson
  • 3,415
  • 10
  • 38
  • 62

6 Answers6

63

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();
})();
Vaviloff
  • 16,282
  • 6
  • 48
  • 56
  • 5
    setViewport not worked for me, but given below code work const browser = await puppeteer.launch( {"headless": false, args: ['--start-maximized'] } ); – Somnath Kadam Oct 19 '19 at 08:43
52

you can user options in launch

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    args:[
       '--start-maximized' // you can also use '--start-fullscreen'
    ]
    });

  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();
})();
Akhil Aravind
  • 5,741
  • 16
  • 35
46
const puppeteer = require('puppeteer-core');

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    defaultViewport: null,
  });

  const pages = await browser.pages();
  const page = pages[0];
  await page.goto('https://google.com');
})();

Source: https://github.com/GoogleChrome/puppeteer/issues/3688#issuecomment-453218745

Chris Xue
  • 2,317
  • 1
  • 25
  • 21
22

Code snippet with related properties

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch(
    {
      headless: false, //defaults to true 
      defaultViewport: null, //Defaults to an 800x600 viewport
      executablePath: 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe', 
                     //by default puppeteer runs Chromium buddled with puppeteer 
      args:['--start-maximized' ]
    });
  const page = await browser.newPage();
  await page.goto('https://example.com');

})();
Sarath
  • 2,719
  • 1
  • 31
  • 39
8

for full screen, i do this:

const puppeteer = require('puppeteer');

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

  browser.close();
})();
Jaber Alshami
  • 336
  • 3
  • 14
5

According to the Puppeteer Documentation:

###page.setViewport(viewport)

  • viewport <Object>
  • width <number> page width in pixels.
  • height <number> page height in pixels.
  • deviceScaleFactor <number> Specify device scale factor (can be thought of as dpr). Defaults to 1.
  • isMobile <boolean> Whether the meta viewport tag is taken into account. Defaults to false.
  • hasTouch <boolean> Specifies if viewport supports touch events. Defaults to false
  • isLandscape <boolean> Specifies if viewport is in landscape mode. Defaults to false.
  • returns: <Promise>

NOTE in certain cases, setting viewport will reload the page in order to set the isMobile or hasTouch properties.

In the case of multiple pages in a single browser, each page can have its own viewport size.

Therefore, you can use page.setViewport() to set the page width and height:

await page.setViewport({
  width: 1366,
  height: 768,
});

Note: Make sure to use the await operator when calling page.setViewport(), as the function is asynchronous.

Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
Grant Miller
  • 27,532
  • 16
  • 147
  • 165