[Python] Python playwright won't let me open user profile other than default profile

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I need to open custom profile using playwright launch_persistent_context method. Here is my code

    import asyncio
    from playwright.async_api import async_playwright
    import os

    async def main():
    # Initialize Playwright and launch a browser
    async with async_playwright() as playwright:
    print("Launching browser")
    browser = await playwright.chromium.launch_persistent_context(user_data_dir=fr'C:\Users\{os.getlogin()}\AppData\Local\Google\Chrome\User Data\Profile 1', headless=False)
    # Create a new page in the browser
    print("Creating new page")
    page = await browser.new_page()
    print("Going to URL")
    # Navigate to a website
    await page.goto("https://www.example.com")

    input("Press Enter to continue...")

    # Close the browser
    await browser.close()

    # Run the main function in an asyncio event loop
    asyncio.run(main())


    The launch_persistent_context method takes an argument called user_data_dir. When I input "User Data" folder to that folder, it launches the browser with default profile. But if I specify some other profile like this user_data_dir="...\User Data\Profile 1", it launches the browser but doesn't load any profile.

    Here is what I tried to get the solution


    1. I read about it on playwright documentation website here, but there isn't much information on how we can open custom profile other than default one.


    2. As mentioned above, I tried to give path to custom profile instead of path to user data directory. But it doesn't load the profile in that case

    Continue reading...

Compartilhe esta Página