Martin Thompson
Martin Thompson

Reputation: 3755

How to maximise Screen use in Pupeteer ( non-headless )

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.

Upvotes: 52

Views: 84862

Answers (6)

Jaber Alshami
Jaber Alshami

Reputation: 404

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();
})();

Upvotes: 9

Grant Miller
Grant Miller

Reputation: 29037

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.

Upvotes: 5

Sarath
Sarath

Reputation: 2844

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');

})();

Upvotes: 27

Chris Xue
Chris Xue

Reputation: 2537

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

Upvotes: 52

Akhil Aravind
Akhil Aravind

Reputation: 6130

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();
})();

Upvotes: 59

Vaviloff
Vaviloff

Reputation: 16856

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();
})();

Upvotes: 68

Related Questions