How to set max viewport in Puppeteer?












1















When I run a new page, I must specify size of the viewport using the setViewport function:



await page.setViewport({
width: 1920,
height: 1080
})


I want use max viewport.



How can I make the viewport resizable according to the window size?










share|improve this question





























    1















    When I run a new page, I must specify size of the viewport using the setViewport function:



    await page.setViewport({
    width: 1920,
    height: 1080
    })


    I want use max viewport.



    How can I make the viewport resizable according to the window size?










    share|improve this question



























      1












      1








      1


      1






      When I run a new page, I must specify size of the viewport using the setViewport function:



      await page.setViewport({
      width: 1920,
      height: 1080
      })


      I want use max viewport.



      How can I make the viewport resizable according to the window size?










      share|improve this question
















      When I run a new page, I must specify size of the viewport using the setViewport function:



      await page.setViewport({
      width: 1920,
      height: 1080
      })


      I want use max viewport.



      How can I make the viewport resizable according to the window size?







      javascript node.js google-chrome-devtools viewport puppeteer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 28 '18 at 19:04









      Grant Miller

      6,339133358




      6,339133358










      asked Sep 28 '18 at 10:23









      fehiffehif

      63




      63
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can pass the --window-size flag as an argument to puppeteer.launch() to change the window size to your desired width and height.



          Then you can call the Chrome Devtools Protocol method Emulation.clearDeviceMetricsOverride to clear the overridden device metrics (including the default 800 x 600 viewport).



          This will cause the viewport to match the window size (when taking screenshots, etc).



          const browser = await puppeteer.launch({
          args : [
          '--window-size=1920,1080'
          ]
          });

          const page = await browser.newPage();

          await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

          await page.screenshot( { path : 'example.png' } ); // Image Dimensions : 1920 x 1080



          Note: page.viewport() will still return { width: 800, height: 600 }, and the only way to reliably change the values of these properties is to use page.setViewport().




          See the complete example below:



          'use strict';

          const puppeteer = require( 'puppeteer' );

          ( async () =>
          {
          /* ============================================================
          Prerequisite: Set Window size
          ============================================================ */

          const browser = await puppeteer.launch({
          args : [
          '--window-size=1920,1080'
          ]
          });

          const page = await browser.newPage();

          await page.goto( 'https://www.example.com/' );

          /* ============================================================
          Case 1: Default Viewport
          ============================================================ */

          console.log( 'Case 1 - Width :', page.viewport().width ); // Width : 800
          console.log( 'Case 1 - Height :', page.viewport().height ); // Height : 600

          await page.screenshot( { path : 'image-1.png' } ); // Image Dimensions : 800 x 600

          /* ============================================================
          Case 2: Clear Overridden Device Metrics
          ============================================================ */

          await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

          console.log( 'Case 2 - Width :', page.viewport().width ); // Width : 800
          console.log( 'Case 2 - Height :', page.viewport().height ); // Height : 600

          await page.screenshot( { path : 'image-2.png' } ); // Image Dimensions : 1920 x 1080

          /* ============================================================
          Case 3: Manually Set Viewport
          ============================================================ */

          await page.setViewport({
          width : 1920,
          height : 1080
          });

          console.log( 'Case 3 - Width :', page.viewport().width ); // Width : 1920
          console.log( 'Case 3 - Height :', page.viewport().height ); // Height : 1080

          await page.screenshot( { path : 'image-3.png' } ); // Image Dimensions : 1920 x 1080

          /* ============================================================
          Case 4: Revert Back to Default Viewport
          ============================================================ */

          await page.setViewport({
          width : 800,
          height : 600
          });

          console.log( 'Case 4 - Width :', page.viewport().width ); // Width : 800
          console.log( 'Case 4 - Height :', page.viewport().height ); // Height : 600

          await page.screenshot( { path : 'image-4.png' } ); // Image Dimensions : 800 x 600

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





          share|improve this answer































            1














            As explained in this issue



            page.setViewport({ width: 0, height: 0 });


            makes chromium set the viewport by inferring the current screen resolution. Seems to only work with headless: false






            share|improve this answer
























            • This one does not work

              – Xin Wang
              Mar 5 at 3:05











            • unfortunate, which one does? worked on my machine :)

              – rivanov
              Mar 5 at 11:45



















            0














            I may be very late on this. Nevertheless for others, try:



            const browser = await puppeteer.launch({defaultViewport: null});


            Set the defaultViewport option to null as above to disable the 800x600 resolution. It takes the max resolution then.






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52553311%2fhow-to-set-max-viewport-in-puppeteer%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can pass the --window-size flag as an argument to puppeteer.launch() to change the window size to your desired width and height.



              Then you can call the Chrome Devtools Protocol method Emulation.clearDeviceMetricsOverride to clear the overridden device metrics (including the default 800 x 600 viewport).



              This will cause the viewport to match the window size (when taking screenshots, etc).



              const browser = await puppeteer.launch({
              args : [
              '--window-size=1920,1080'
              ]
              });

              const page = await browser.newPage();

              await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

              await page.screenshot( { path : 'example.png' } ); // Image Dimensions : 1920 x 1080



              Note: page.viewport() will still return { width: 800, height: 600 }, and the only way to reliably change the values of these properties is to use page.setViewport().




              See the complete example below:



              'use strict';

              const puppeteer = require( 'puppeteer' );

              ( async () =>
              {
              /* ============================================================
              Prerequisite: Set Window size
              ============================================================ */

              const browser = await puppeteer.launch({
              args : [
              '--window-size=1920,1080'
              ]
              });

              const page = await browser.newPage();

              await page.goto( 'https://www.example.com/' );

              /* ============================================================
              Case 1: Default Viewport
              ============================================================ */

              console.log( 'Case 1 - Width :', page.viewport().width ); // Width : 800
              console.log( 'Case 1 - Height :', page.viewport().height ); // Height : 600

              await page.screenshot( { path : 'image-1.png' } ); // Image Dimensions : 800 x 600

              /* ============================================================
              Case 2: Clear Overridden Device Metrics
              ============================================================ */

              await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

              console.log( 'Case 2 - Width :', page.viewport().width ); // Width : 800
              console.log( 'Case 2 - Height :', page.viewport().height ); // Height : 600

              await page.screenshot( { path : 'image-2.png' } ); // Image Dimensions : 1920 x 1080

              /* ============================================================
              Case 3: Manually Set Viewport
              ============================================================ */

              await page.setViewport({
              width : 1920,
              height : 1080
              });

              console.log( 'Case 3 - Width :', page.viewport().width ); // Width : 1920
              console.log( 'Case 3 - Height :', page.viewport().height ); // Height : 1080

              await page.screenshot( { path : 'image-3.png' } ); // Image Dimensions : 1920 x 1080

              /* ============================================================
              Case 4: Revert Back to Default Viewport
              ============================================================ */

              await page.setViewport({
              width : 800,
              height : 600
              });

              console.log( 'Case 4 - Width :', page.viewport().width ); // Width : 800
              console.log( 'Case 4 - Height :', page.viewport().height ); // Height : 600

              await page.screenshot( { path : 'image-4.png' } ); // Image Dimensions : 800 x 600

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





              share|improve this answer




























                1














                You can pass the --window-size flag as an argument to puppeteer.launch() to change the window size to your desired width and height.



                Then you can call the Chrome Devtools Protocol method Emulation.clearDeviceMetricsOverride to clear the overridden device metrics (including the default 800 x 600 viewport).



                This will cause the viewport to match the window size (when taking screenshots, etc).



                const browser = await puppeteer.launch({
                args : [
                '--window-size=1920,1080'
                ]
                });

                const page = await browser.newPage();

                await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

                await page.screenshot( { path : 'example.png' } ); // Image Dimensions : 1920 x 1080



                Note: page.viewport() will still return { width: 800, height: 600 }, and the only way to reliably change the values of these properties is to use page.setViewport().




                See the complete example below:



                'use strict';

                const puppeteer = require( 'puppeteer' );

                ( async () =>
                {
                /* ============================================================
                Prerequisite: Set Window size
                ============================================================ */

                const browser = await puppeteer.launch({
                args : [
                '--window-size=1920,1080'
                ]
                });

                const page = await browser.newPage();

                await page.goto( 'https://www.example.com/' );

                /* ============================================================
                Case 1: Default Viewport
                ============================================================ */

                console.log( 'Case 1 - Width :', page.viewport().width ); // Width : 800
                console.log( 'Case 1 - Height :', page.viewport().height ); // Height : 600

                await page.screenshot( { path : 'image-1.png' } ); // Image Dimensions : 800 x 600

                /* ============================================================
                Case 2: Clear Overridden Device Metrics
                ============================================================ */

                await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

                console.log( 'Case 2 - Width :', page.viewport().width ); // Width : 800
                console.log( 'Case 2 - Height :', page.viewport().height ); // Height : 600

                await page.screenshot( { path : 'image-2.png' } ); // Image Dimensions : 1920 x 1080

                /* ============================================================
                Case 3: Manually Set Viewport
                ============================================================ */

                await page.setViewport({
                width : 1920,
                height : 1080
                });

                console.log( 'Case 3 - Width :', page.viewport().width ); // Width : 1920
                console.log( 'Case 3 - Height :', page.viewport().height ); // Height : 1080

                await page.screenshot( { path : 'image-3.png' } ); // Image Dimensions : 1920 x 1080

                /* ============================================================
                Case 4: Revert Back to Default Viewport
                ============================================================ */

                await page.setViewport({
                width : 800,
                height : 600
                });

                console.log( 'Case 4 - Width :', page.viewport().width ); // Width : 800
                console.log( 'Case 4 - Height :', page.viewport().height ); // Height : 600

                await page.screenshot( { path : 'image-4.png' } ); // Image Dimensions : 800 x 600

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





                share|improve this answer


























                  1












                  1








                  1







                  You can pass the --window-size flag as an argument to puppeteer.launch() to change the window size to your desired width and height.



                  Then you can call the Chrome Devtools Protocol method Emulation.clearDeviceMetricsOverride to clear the overridden device metrics (including the default 800 x 600 viewport).



                  This will cause the viewport to match the window size (when taking screenshots, etc).



                  const browser = await puppeteer.launch({
                  args : [
                  '--window-size=1920,1080'
                  ]
                  });

                  const page = await browser.newPage();

                  await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

                  await page.screenshot( { path : 'example.png' } ); // Image Dimensions : 1920 x 1080



                  Note: page.viewport() will still return { width: 800, height: 600 }, and the only way to reliably change the values of these properties is to use page.setViewport().




                  See the complete example below:



                  'use strict';

                  const puppeteer = require( 'puppeteer' );

                  ( async () =>
                  {
                  /* ============================================================
                  Prerequisite: Set Window size
                  ============================================================ */

                  const browser = await puppeteer.launch({
                  args : [
                  '--window-size=1920,1080'
                  ]
                  });

                  const page = await browser.newPage();

                  await page.goto( 'https://www.example.com/' );

                  /* ============================================================
                  Case 1: Default Viewport
                  ============================================================ */

                  console.log( 'Case 1 - Width :', page.viewport().width ); // Width : 800
                  console.log( 'Case 1 - Height :', page.viewport().height ); // Height : 600

                  await page.screenshot( { path : 'image-1.png' } ); // Image Dimensions : 800 x 600

                  /* ============================================================
                  Case 2: Clear Overridden Device Metrics
                  ============================================================ */

                  await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

                  console.log( 'Case 2 - Width :', page.viewport().width ); // Width : 800
                  console.log( 'Case 2 - Height :', page.viewport().height ); // Height : 600

                  await page.screenshot( { path : 'image-2.png' } ); // Image Dimensions : 1920 x 1080

                  /* ============================================================
                  Case 3: Manually Set Viewport
                  ============================================================ */

                  await page.setViewport({
                  width : 1920,
                  height : 1080
                  });

                  console.log( 'Case 3 - Width :', page.viewport().width ); // Width : 1920
                  console.log( 'Case 3 - Height :', page.viewport().height ); // Height : 1080

                  await page.screenshot( { path : 'image-3.png' } ); // Image Dimensions : 1920 x 1080

                  /* ============================================================
                  Case 4: Revert Back to Default Viewport
                  ============================================================ */

                  await page.setViewport({
                  width : 800,
                  height : 600
                  });

                  console.log( 'Case 4 - Width :', page.viewport().width ); // Width : 800
                  console.log( 'Case 4 - Height :', page.viewport().height ); // Height : 600

                  await page.screenshot( { path : 'image-4.png' } ); // Image Dimensions : 800 x 600

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





                  share|improve this answer













                  You can pass the --window-size flag as an argument to puppeteer.launch() to change the window size to your desired width and height.



                  Then you can call the Chrome Devtools Protocol method Emulation.clearDeviceMetricsOverride to clear the overridden device metrics (including the default 800 x 600 viewport).



                  This will cause the viewport to match the window size (when taking screenshots, etc).



                  const browser = await puppeteer.launch({
                  args : [
                  '--window-size=1920,1080'
                  ]
                  });

                  const page = await browser.newPage();

                  await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

                  await page.screenshot( { path : 'example.png' } ); // Image Dimensions : 1920 x 1080



                  Note: page.viewport() will still return { width: 800, height: 600 }, and the only way to reliably change the values of these properties is to use page.setViewport().




                  See the complete example below:



                  'use strict';

                  const puppeteer = require( 'puppeteer' );

                  ( async () =>
                  {
                  /* ============================================================
                  Prerequisite: Set Window size
                  ============================================================ */

                  const browser = await puppeteer.launch({
                  args : [
                  '--window-size=1920,1080'
                  ]
                  });

                  const page = await browser.newPage();

                  await page.goto( 'https://www.example.com/' );

                  /* ============================================================
                  Case 1: Default Viewport
                  ============================================================ */

                  console.log( 'Case 1 - Width :', page.viewport().width ); // Width : 800
                  console.log( 'Case 1 - Height :', page.viewport().height ); // Height : 600

                  await page.screenshot( { path : 'image-1.png' } ); // Image Dimensions : 800 x 600

                  /* ============================================================
                  Case 2: Clear Overridden Device Metrics
                  ============================================================ */

                  await page._client.send( 'Emulation.clearDeviceMetricsOverride' );

                  console.log( 'Case 2 - Width :', page.viewport().width ); // Width : 800
                  console.log( 'Case 2 - Height :', page.viewport().height ); // Height : 600

                  await page.screenshot( { path : 'image-2.png' } ); // Image Dimensions : 1920 x 1080

                  /* ============================================================
                  Case 3: Manually Set Viewport
                  ============================================================ */

                  await page.setViewport({
                  width : 1920,
                  height : 1080
                  });

                  console.log( 'Case 3 - Width :', page.viewport().width ); // Width : 1920
                  console.log( 'Case 3 - Height :', page.viewport().height ); // Height : 1080

                  await page.screenshot( { path : 'image-3.png' } ); // Image Dimensions : 1920 x 1080

                  /* ============================================================
                  Case 4: Revert Back to Default Viewport
                  ============================================================ */

                  await page.setViewport({
                  width : 800,
                  height : 600
                  });

                  console.log( 'Case 4 - Width :', page.viewport().width ); // Width : 800
                  console.log( 'Case 4 - Height :', page.viewport().height ); // Height : 600

                  await page.screenshot( { path : 'image-4.png' } ); // Image Dimensions : 800 x 600

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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 28 '18 at 19:03









                  Grant MillerGrant Miller

                  6,339133358




                  6,339133358

























                      1














                      As explained in this issue



                      page.setViewport({ width: 0, height: 0 });


                      makes chromium set the viewport by inferring the current screen resolution. Seems to only work with headless: false






                      share|improve this answer
























                      • This one does not work

                        – Xin Wang
                        Mar 5 at 3:05











                      • unfortunate, which one does? worked on my machine :)

                        – rivanov
                        Mar 5 at 11:45
















                      1














                      As explained in this issue



                      page.setViewport({ width: 0, height: 0 });


                      makes chromium set the viewport by inferring the current screen resolution. Seems to only work with headless: false






                      share|improve this answer
























                      • This one does not work

                        – Xin Wang
                        Mar 5 at 3:05











                      • unfortunate, which one does? worked on my machine :)

                        – rivanov
                        Mar 5 at 11:45














                      1












                      1








                      1







                      As explained in this issue



                      page.setViewport({ width: 0, height: 0 });


                      makes chromium set the viewport by inferring the current screen resolution. Seems to only work with headless: false






                      share|improve this answer













                      As explained in this issue



                      page.setViewport({ width: 0, height: 0 });


                      makes chromium set the viewport by inferring the current screen resolution. Seems to only work with headless: false







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 21 '18 at 17:34









                      rivanovrivanov

                      482714




                      482714













                      • This one does not work

                        – Xin Wang
                        Mar 5 at 3:05











                      • unfortunate, which one does? worked on my machine :)

                        – rivanov
                        Mar 5 at 11:45



















                      • This one does not work

                        – Xin Wang
                        Mar 5 at 3:05











                      • unfortunate, which one does? worked on my machine :)

                        – rivanov
                        Mar 5 at 11:45

















                      This one does not work

                      – Xin Wang
                      Mar 5 at 3:05





                      This one does not work

                      – Xin Wang
                      Mar 5 at 3:05













                      unfortunate, which one does? worked on my machine :)

                      – rivanov
                      Mar 5 at 11:45





                      unfortunate, which one does? worked on my machine :)

                      – rivanov
                      Mar 5 at 11:45











                      0














                      I may be very late on this. Nevertheless for others, try:



                      const browser = await puppeteer.launch({defaultViewport: null});


                      Set the defaultViewport option to null as above to disable the 800x600 resolution. It takes the max resolution then.






                      share|improve this answer






























                        0














                        I may be very late on this. Nevertheless for others, try:



                        const browser = await puppeteer.launch({defaultViewport: null});


                        Set the defaultViewport option to null as above to disable the 800x600 resolution. It takes the max resolution then.






                        share|improve this answer




























                          0












                          0








                          0







                          I may be very late on this. Nevertheless for others, try:



                          const browser = await puppeteer.launch({defaultViewport: null});


                          Set the defaultViewport option to null as above to disable the 800x600 resolution. It takes the max resolution then.






                          share|improve this answer















                          I may be very late on this. Nevertheless for others, try:



                          const browser = await puppeteer.launch({defaultViewport: null});


                          Set the defaultViewport option to null as above to disable the 800x600 resolution. It takes the max resolution then.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 12 hours ago









                          double-beep

                          2,95041329




                          2,95041329










                          answered 12 hours ago









                          n00b_g33kn00b_g33k

                          11




                          11






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52553311%2fhow-to-set-max-viewport-in-puppeteer%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to change which sound is reproduced for terminal bell?

                              Can I use Tabulator js library in my java Spring + Thymeleaf project?

                              Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents