[Python] context.route_from_har is both saving as har-file and extracting some networking-log

Discussão em 'Python' iniciado por Stack, Agosto 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to save the har-log of a session with playwright with

    context.route_from_har(har=path_har, update=True)


    I want it only as a single har-file but it also pollutes the directory of the script with files like JS-sripts, html, css, images, ... of the form 817ea1d1972ae248dc9eabba87e8d2256f84959d.js,

    The program is executed as (venv)$ python app.py and the details of the code is below

    from playwright.sync_api import sync_playwright


    url = # some url
    path_har = './session.har'

    with sync_playwright() as p:
    browser_args = (
    ...
    )

    browser = p.chromium.launch(
    headless=False,
    devtools=False, # only works when headless=False
    ignore_default_args=browser_args
    )

    context = browser.new_context(
    ...
    )

    context.route_from_har(har=path_har, update=True)

    page = context.new_page()
    page.goto(url)

    context.close() # close it to save har-file
    browser.close()


    Inspecting the directory content

    (venv)$ ls - 1
    817ea1d1972ae248dc9eabba87e8d2256f84959d.js
    85245ea78118d39e8d9d4bc1ab216228e5d66ce2.html
    a4ec070813be6ef3584dc6c1fbe2bf411d2833bf.css
    [...]
    session.har
    app.py

    Continue reading...

Compartilhe esta Página