How do I set the page viewport size using Scrapy Playwight?

390 Views Asked by At

I didn't find any satisfactory answer about this. All I want is just set the viewport to 1080*19200 (yes, 1920 * 10) before the request to simulate a screen of that size.

Is this even possible using Scrapy Playwright? If so, how do i do that?

1

There are 1 best solutions below

0
elacuesta On

There are multiple ways of doing this:

  1. In the context. Playwright's Browser.new_context method takes a viewport argument, which can be used in the PLAYWRIGHT_CONTEXTS setting:
# settings.py
PLAYWRIGHT_CONTEXTS = {
    "default": {
        "viewport": {
            "width": 19200,
            "height": 1080,
        },
    },
}
  1. In the page. You can call Page.set_viewport_size from a page init callback:
async def init_page(page, request):
    await page.set_viewport_size({"width": 19200, "height": 1080})
yield scrapy.Request(
    url="https://example.org",
    meta={
        "playwright": True,
        "playwright_page_init_callback": init_page,
    },
)
  1. In the page, with a PageMethod:
from scrapy_playwright.page import PageMethod
yield scrapy.Request(
    url="https://example.org",
    meta={
        "playwright": True,
        "playwright_page_methods": [
            PageMethod("set_viewport_size", {"width": 19200, "height": 1080}),
        ],
    },
)