Start playwright driver session but not incognito
Asked Answered
P

2

7

Currently i open a new browser session using the code below, but it always starts as incognito, can I start a new chromium session but not as incognito?:

from behave import *
from playwright.sync_api import sync_playwright
import time


class session_driver:
    driver = None

    def open_browser(self, url):
        playW_sync_instace = sync_playwright().start()
        global browser
        browser = playW_sync_instace.chromium.launch(headless=False)
        browser.new_context(record_video_dir="videos/",
        record_video_size={"width": 640, "height": 480})
        self.driver = browser.new_page()
        self.driver.goto(url)
Piracy answered 20/9, 2021 at 14:8 Comment(1)
If you want peristency, you can use chromium.launch_persistent_context, see here: playwright.dev/python/docs/api/… This allows you to save the storage state (cookies, etc.) to the local disk and re-use it multiple times.Uda
C
9
from playwright.sync_api import sync_playwright
import os
user_dir = '/tmp/playwright'

if not os.path.exists(user_dir):
  os.makedirs(user_dir)

with sync_playwright() as p:
  browser = p.chromium.launch_persistent_context(user_dir, headless=False)
  page = browser.new_page()
  page.goto('https://playwright.dev/python', wait_until='domcontentloaded')
Caretaker answered 27/10, 2021 at 17:49 Comment(0)
M
0

This is what I have done in Typescript code base. But this would leverage the existing logged-in session, and it would not ask for fresh user login.

const userDataDir = 'C:/Users/yuv****dir/AppData/Local/Temp/tjwmm3m0.hmt';

context = await chromium.launchPersistentContext(userDataDir,{ headless: false, args: [ ] });

I hope this is like above only what is there in Python code.

Macro answered 10/5, 2022 at 16:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.