Is there a way to connect to my existing browser session using playwright

Viewed 2042

I wish to connect to a website and download some pdf files. The website allows us to view the content only after log in. It asks us to log in using OTP and can't be login at more than 3 devices simultaneously.

I wish to download all the pdf listed. So I previously tried the

python playwright open --save-storage websitename.json

to save the login. But it doesn't work for that specific website. The website.json file was empty whereas it worked for other websites.

Therefore the only solution I could think of know, is to connect to the current browser, open that website and then download those pdfs.

If you have some solution for this or even some other approach please do inform.

I was also thinking about switching over to puppeteer for the same. But, I don't know the html parsing using node.js, since I feel using css selectors more comfortable, so I can't switch it.

1 Answers

Playwright is basically same as Puppeteer. So it wouldn't be a problem if you switch between the two.
You can use puppeteer-core or playwright to control your existing browser installation, for example Chrome, and then use the existing user data (Profile) folder to load the specified website login info (cookies, webstorage, etc).

const launchOptions = {
    headless: false,
    executablePath: '/Applications/Google Chrome/Contents/MacOS/Google Chrome', // For MacOS
    // executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', // For Windows
    // executablePath: '/usr/bin/google-chrome'  // For Linux
    args: [
        '--user-data-dir=/Users/username/Library/Application Support/Google/Chrome/', // For MacOS
        // '--user-data-dir=%userprofile%\\AppData\\Local\\Chrome\\User Data', // For Windows
        // '--profile-directory=Profile 1' // This to select default or specified Profile
    ]
}
const puppeteer = require('puppeteer-core')
const browser = await puppeteer.launch(launchOptions)

For more details about Playwright's method, you can check this workaround: https://github.com/microsoft/playwright/issues/1985

Related