5

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)
Gabi
  • 51
  • 1
  • 4
  • 3
    If you want peristency, you can use chromium.launch_persistent_context, see here: https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context This allows you to save the storage state (cookies, etc.) to the local disk and re-use it multiple times. – Max Schmitt Sep 21 '21 at 09:15

2 Answers2

7
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')
Kayce Basques
  • 23,849
  • 11
  • 86
  • 120
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.

yuvraj
  • 181
  • 1
  • 12