can i run playwright outside of 'with'?

how can i run

browser = sync_playwright().chromium.launch()

according to documentation, playwrite must be run with with

with sync_playwrith as p:

what causes my browser to close as soon as 'with' ends

taking sync_playwright() out of with

I want my instance not to be closed to be reused when an event happens

I could do that?

my code scenario is as follows: I open the browser I log in to the site, and I wait for an entry that comes through a telegram channel, when this entry happens, it is processed and generates actions on the site that reusing the entire instance of the browser already open and login already performed at startup

Upvotes: 1

Views: 1337

Answers (1)

Isara De Silva
Isara De Silva

Reputation: 76

You can get an idea from the REPL

And what I have up and running is:

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=True)
page = browser.new_page()
page.goto(<URL>)

# Can perform your logic here after that it will close.
# Or else you can pass the "page" variable to a separate function

if page:
    page.close()

if browser:
    browser.close()

if playwright:
    playwright.stop()

Upvotes: 3

Related Questions