How to set viewport / browser size

I searched all the documentation but I couldn't find anywhere how to set viewport. I'm especially interested in this in the context of PlaywrightCrawler. I don't need a specific answer, just outline how to find it.
5 Replies
hard-chocolate
hard-chocolate3y ago
you can use preNavigationHooks option to set it:
preNavigationHooks: [
async ({ page }) => {
await page.setViewportSize({ width: 1920, height: 1080 })
},
],
preNavigationHooks: [
async ({ page }) => {
await page.setViewportSize({ width: 1920, height: 1080 })
},
],
playwright docs: https://playwright.dev/docs/api/class-page#page-set-viewport-size
ratty-blush
ratty-blush3y ago
Should also be possible with somewhat like that (not :
launchContext: {
launchOptions: {
viewport: { width: 1920, height: 1080 },
},
}
launchContext: {
launchOptions: {
viewport: { width: 1920, height: 1080 },
},
}
which is internally using https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context-option-viewport
BrowserType | Playwright
BrowserType provides methods to launch a specific browser instance or connect to an existing one. The following is a typical example of using Playwright to drive automation:
harsh-harlequin
harsh-harlequin3y ago
Per discussion with @petrpatek. , it is better to set this in https://crawlee.dev/api/browser-pool/interface/FingerprintOptions (missing correct type) so it is in line with fingerprint
correct-apricot
correct-apricot2y ago
This doesn't seem to work - trying to specify viewport in launchOptions generates a type error. Has it been deprecated or is there some other way to set it? Full code that generates the error:
const crawler = new PlaywrightCrawler({
requestHandler: router,
// Uncomment this option to see the browser window.
headless: false,
browserPoolOptions: {
maxOpenPagesPerBrowser: 7,
},
launchContext: {
launchOptions: {
viewport: { width: 1920, height: 1080 },
},
useChrome: true,
},
// Let's limit our crawls to make our tests shorter and safer.
maxRequestsPerCrawl: 5,
requestHandlerTimeoutSecs: 360,
})
const crawler = new PlaywrightCrawler({
requestHandler: router,
// Uncomment this option to see the browser window.
headless: false,
browserPoolOptions: {
maxOpenPagesPerBrowser: 7,
},
launchContext: {
launchOptions: {
viewport: { width: 1920, height: 1080 },
},
useChrome: true,
},
// Let's limit our crawls to make our tests shorter and safer.
maxRequestsPerCrawl: 5,
requestHandlerTimeoutSecs: 360,
})
hard-chocolate
hard-chocolate2y ago
Did you try to set it in preNavigationHooks or in https://crawlee.dev/api/browser-pool/interface/FingerprintOptions ? (Examples above)

Did you find this page helpful?