-
Notifications
You must be signed in to change notification settings - Fork 9.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
window inner size not equal to viewport size #1183
Comments
It has a param can be reset the window viewports in launch method.
…Sent from my iPhone
On 29 Oct 2017, at 5:41 AM, Abu Taher ***@***.***> wrote:
To add more salt into it, opening devtools and closing it will make the viewport lose the predefined size. For example,
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@zicongxie |
@entrptaher I noticed that too. |
@chanjsq what's your usecase? Why would you need to change the window size? |
For me, say I will want to test responsiveness. |
@aslushnikov When setting the viewport size larger than the default window size, we also need to set the window size with the same value to see the whole page width, it's convenient if the window size can change with viewport size accordingly. |
Have the same issue on Windows 8. |
@entrptaher isn't viewport enough for responsiveness? |
@aslushnikov yes, smart can be better |
can i make the viewport equal to the size of the window by default, and adjustable while resizing? i don't want the viewport which is not equal to the window size :( +1 on this one |
I'm using this code: await page.setViewport({height, width});
// Window frame - probably OS and WM dependent.
height += 85;
// Any tab.
const {targetInfos: [{targetId}]} = await browser._connection.send(
'Target.getTargets'
);
// Tab window.
const {windowId} = await browser._connection.send(
'Browser.getWindowForTarget',
{targetId}
);
// Resize.
await browser._connection.send('Browser.setWindowBounds', {
bounds: {height, width},
windowId
}); I think that browser.resize({height, width}) |
Definitely also need this, fwiw. |
this function is very necessar |
+1 |
https://peter.sh/experiments/chromium-command-line-switches/#window-size
|
I am using the following to achieve the desired effect.
|
Based on https://github.com/GoogleChrome/puppeteer/blob/v0.13.0/docs/api.md#pagefocusselector and @Mehuge asnwer const chrome = { x: 0, y: 74 }; // comes from config in reality
args.push(`--window-size=${width+chrome.x},${height+chrome.y}`);
puppeteer.launch({ headless, sloMo, args });
// ...
page.setViewport({ width, height }); (correcting typo in |
Thanks, guys!!! I combined @akylkb with @Mehuge and got the perfect solution! And fwiw, for people who didn't understand why viewport size is important. It was stopping me dead in my tracks, because a common practice in many websites is to change the layout from pc / laptop to mobile based on viewport size, and the default size was causing it to go to the mobile layout, which did not have the login button available without an extra menu, involving two extra clicks. The entire DOM and navigation path can be completely different depending on how many different responsive designs the website designers have in place; they could have several, from smart watches to mobile, to ipads and tablets, touch screens and kiosks as well. The world includes much, much more than just laptops and pcs, and each and every layout will have a completely different DOM structure. So, again, THANK YOU, @akylkb and @Mehuge, your solutions deserve a lot more attention imo!! |
Think this is actually a bug when connecting over a websocket as I can't find a way to set the window size when on web socket. This means that some sites render incorrectly as they seem to look at the window size and not that set viewport, i.e: I'm not 100% sure of this, but would seem to be the case and would appreciate anybody who could confirm. |
Hi, thx guys for solutions, but screen.width and screen.height are in
|
* Workaround for puppeteer/puppeteer#1183 * Formatting. * Formatting.
This might be related issue, When saving as PDF (have to use headless mode), the window size triggers the incorrect media query. For example, if
|
This patch: - teaches page.setViewpot() to accept `null` to disable viewport emulation. - allows passing `0` as deviceScaleFactor to use machine-defined deviceScaleFactor. References puppeteer#1183. Fixes puppeteer#2358.
I am now using @radekmie's solution, as well as the --window-size argument. In my test environment, the browser size may be specified before the browser window is open, and may be altered afterwards. So in the case where it is specified before the browser window is open, I remember the size and use --window-size when launching the browser. If the test scripts later specify a new size, I then use the resize code provided by @radekmie. |
@DanielRuf Thanks, my version of puppeteer is the latest version(1.8.0). |
I'm facing the same issue as above. |
Mh weird, it seems this was not resolved as I am still getting the narrow / small viewport even if I resize. Is there some regression? |
I want to share my personal experience. This will be a long trip. I'm using puppeteer@1.8.0 and Google Chrome V69.0.3497.100 in a Windows10 environment. Let's say I am new to Headless Chrome, and I want to print a dashboard in PDF. I searched for a typical responsive template dashboard and found this one I will be using as an example, credits to the original author: https://demos.creative-tim.com/bs3/paper-dashboard/dashboard.html Then I create a new fresh script to work with: const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false}) // debug
const page = await browser.newPage()
await page.setViewport({width: 768, height: 1024, deviceScaleFactor: 2})
await page.goto('https://demos.creative-tim.com/bs3/paper-dashboard/dashboard.html', {waitUntil: 'networkidle0'})
await page.waitFor(1000)
await page.click('button.close') // remove notification
await page.click('a.navbar-brand') // click on page to close advertisement
await page.emulateMedia('screen') // force screen CSS
await page.waitFor(1000) // wait for every modal to dissapear
await page.pdf({path: `${Date.now()}.pdf`, format: 'A4', printBackground: true})
await browser.close()
})() After that, I prove that in headful mode, and it gives me this result: Firstly, why that blank space? Is that the difference between the default 800px width viewport minus my 768px viewport? I don't think so. However, this does not show in the final PDF, only in headful mode, but it stills bugs me a lot. In headless mode, it shows at follows: Secondly, why the media CSS flex is not triggering as showed in the intial picture? There should be two columned layout, not the single one. As I inspect them, they show as classic '@media' queries. Maybe the 'screen' forced mode instead of 'print' is causing this? I do not know. Thirdly, why my pixel resolution is not being respected? If, in headful mode, I show the DevTools and extract them from the page (which seems, by the way, to re-render the page to the expected behaviour), I can see the pixel resolution is not at all the one I setted of in the initial step. In fact, I could even increase the pixel resolution to have no results on the output: Ah, that seems the old 800x600 viewport issue we were talking about. So I started to test every solution in this post, with these results:
Maybe the float is affecting the behaviour? But again, why am I not achieveing the exact same result being in headful or in headless, as this is the whole point? I do not know. That will be an entirely different issue. |
Is there any way we can get this opened? It's still an issue, and @fergardi's comment describes the issue nicely. cc @aslushnikov |
I was knocking my head over this for some time, until i realised (at least for my particular use case) is that this is all i needed to do to break out of the default 800 x 600 size: const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulateMedia("screen"); // <- optional but super useful
await page.goto("http://localhost:3000", {
waitUntil: "networkidle2"
});
await page.pdf({
path: "filename.pdf",
pageRanges: "1",
width: 1600, // <- set width
height: 1240, // <- set height
printBackground: true
});
})(); |
why is this issue closed, while the problem is still there? |
tested, still an issue. |
Everybody: the reason this issue is closed is because you can actually disable viewport emulation with Here's an example: const browser = puppeteer.launch({
defaultViewport: null, // <= set this to have viewport emulation off
});
const page = await browser.newPage();
// ... |
thanks for the tip @aslushnikov |
FWIW My workaround was to add:
This way the view port is always set the size I want. You can go a step further and read out the values from the config file. |
As of today @aslushnikov method still works for anyone having this problem. const browser = puppeteer.launch({
defaultViewport: null, // <= set this to have viewport emulation off
}); |
THANK YOU SO MUCH YOU ARE AN ANGEL |
having fought with this for a few days, none of the googled solutions worked for me. However...
Executing this before navigating to the target URL will allow a script on that page to properly read the window size. |
Started working with puppeteer since few days, struggled with this problem. But this solution worked like a magic. Thanks @aslushnikov
|
I´m using Puppeteer and Jest and this jest-puppeteer.config.js works for me. :)
|
a little addition
|
#> this.helpers.Puppeteer.resizeWindow(1200, 960)
Where did you add these lines? |
这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
|
这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
|
这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
4 years later, you made my day sir! Thanks! |
这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
That Chinese guy is on vacation for 2 years already |
Tell us about your environment:
What steps will reproduce the problem?
Please include code that reproduces the issue.
node start.js
What is the expected result?
What happens instead?
The text was updated successfully, but these errors were encountered: