Python Playwright start maximized window
Asked Answered
F

4

6

I have a problem starting Playwright in Python maximized. I found some articles for other languages but doesn't work in Python, also nothing is written about maximizing window in Python in the official documentation.

I tried browser = p.chromium.launch(headless=False, args=["--start-maximized"])

And it starts maximized but then automatically restores back to the default small window size.

Any ideas? Thanks

Frances answered 17/1, 2023 at 9:3 Comment(3)
Does this answer your question? Playwright - Javascript - Maximize BrowserHalicarnassus
Thanks, yes this is the fix, it is same as my answer below.Frances
See this - stackoverflow.com/a/77281854Androecium
F
4

I just found the answer:

I need to set also the following and it works: browser.new_context(no_viewport=True)

Frances answered 17/1, 2023 at 9:11 Comment(0)
G
6

I'm using playwright and pytest-playwright.

def test_foo(playwright):
    browser = playwright.chromium.launch(headless=False, args=["--start-maximized"])
    # create a new incognito browser context.
    context = browser.new_context(no_viewport=True)
    # create a new page in a pristine context.
    page = context.new_page()
    page.goto("https://example.com")

Run tests with pytest ., no need to specify the browser and headless params. With new_context it won't share cookies/cache with other browser contexts so we get separation of test runs

Gibeon answered 9/4, 2023 at 10:12 Comment(0)
F
4

I just found the answer:

I need to set also the following and it works: browser.new_context(no_viewport=True)

Frances answered 17/1, 2023 at 9:11 Comment(0)
O
2

This is the way you should do it and it works

with sync_playwright() as p:
    browser = p.chromium.launch(args=['--start-maximized'], headless=False)
    page = browser.new_page(no_viewport=True)
    page.goto(url)
Orpha answered 21/10, 2023 at 10:33 Comment(0)
F
1

You could create a conftest.py and define:

def page(playwright: Playwright, scope="module"):
    app_data_path = os.getenv("LOCALAPPDATA")
    chromium_path = "ms-playwright\\chromium-1028\\chrome-win"
    chromium_path = os.path.join(app_data_path, chromium_path)
    chrome = playwright.chromium\
        .launch_persistent_context(chromium_path, headless=False, args=["--start-maximised"], no_viewport=True)
    page = chrome.new_page()
    return page

Then, will be able manually maximize the window and won't restore is back

Fortaleza answered 14/3, 2023 at 13:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.