How to set max viewport in Puppeteer?
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
add a comment |
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
add a comment |
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
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
javascript node.js google-chrome-devtools viewport puppeteer
edited Sep 28 '18 at 19:04
Grant Miller
6,339133358
6,339133358
asked Sep 28 '18 at 10:23
fehiffehif
63
63
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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 usepage.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();
})();
add a comment |
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
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 usepage.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();
})();
add a comment |
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 usepage.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();
})();
add a comment |
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 usepage.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();
})();
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 usepage.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();
})();
answered Sep 28 '18 at 19:03
Grant MillerGrant Miller
6,339133358
6,339133358
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 12 hours ago
double-beep
2,95041329
2,95041329
answered 12 hours ago
n00b_g33kn00b_g33k
11
11
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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