Search code examples
pythonplaywright

Python playwright - existing browser and variable in function


Below is the code of my web scraper, where I am trying to scrape the car names. I have also have list with locations. I would like to use the variable loc of the locationlist in my function.

And also use the opened browser window, but the function does not recognize the syntax 'page.' that is on line 16.

F.e. like Selenium where you define your webdriver once at the top of your code and use it in and outside any function without having call it again.

How can I use the page in my function -page.goto("https://www.sixt.co.uk/")-

from playwright.sync_api import Playwright, sync_playwright, expect

locationList = [
    'London Luton Airport',
    'London Hilton',
    'London City',
    'London Wembley',
    'London Battersea',
    'London Shepherds Bush'
]

chromium = playwright.chromium
browser = chromium.launch(channel="chrome", headless=False)
page = browser.new_page()
page.set_viewport_size({"width": 1920, "height": 1080})
page.goto("https://www.sixt.co.uk/")
page.locator("[data-testid=\"uc-accept-all-button\"]").click()
page.locator("[placeholder=\"Find a location\"]").click()
page.locator("[placeholder=\"Find a location\"]").fill("luton")
page.locator("text=London Luton Airport (GB)").click()
page.locator("button:has-text(\"Show offers\")").click()

def run(playwright: Playwright):

    page.locator("[placeholder=\"Find a location\"]").click()
    page.locator("[placeholder=\"Find a location\"]").fill(loc)
    page.locator('//div[text()="' + loc + '"]').click()
    carnames = page.locator("//h2[@class='vehicle-item__title']")
    #Get all carnames in a list
    carnamelist = [cars.text for cars in carnames]

for loc in locationList:
    with sync_playwright() as playwright:
        run(playwright)

Solution

  • Here's how to initialize playwright browser globally without context managers.

    In your particular code, this will work like below:

    from playwright.sync_api import Playwright, sync_playwright
    
    locationList = [
        'London Luton Airport',
        'London Hilton',
        'London City',
        'London Wembley',
        'London Battersea',
        'London Shepherds Bush'
    ]
    
    playwright = sync_playwright().start()  # <-- Use this to initialize playwright globally
    
    chromium = playwright.chromium
    browser = chromium.launch(channel="chrome", headless=False)
    page = browser.new_page()
    page.set_viewport_size({"width": 1920, "height": 1080})
    page.goto("https://www.sixt.co.uk/")
    page.locator("[data-testid=\"uc-accept-all-button\"]").click()
    page.locator("[placeholder=\"Find a location\"]").click()
    page.locator("[placeholder=\"Find a location\"]").fill("luton")
    page.locator("text=London Luton Airport (GB)").click()
    page.locator("button:has-text(\"Show offers\")").click()
    
    def run(playwright: Playwright):
    
        page.locator("[placeholder=\"Find a location\"]").click()
        page.locator("[placeholder=\"Find a location\"]").fill(loc)
        page.locator('//div[text()="' + loc + '"]').click()
        carnames = page.locator("//h2[@class='vehicle-item__title']")
        #Get all carnames in a list
        carnamelist = [cars.text for cars in carnames]
    
    for loc in locationList:
        run(playwright)
    
    playwright.stop()  # --> Cleanup resources properly when done
    

    However, you can also initialize page and browser from inside the context manager you create in the original question, and it will work as expected.