Skip to content

How to block resources in Playwright and Python

Playwright lets you intercept outgoing requests before they reach the network.

OUTPUT FORMATSHTMLJSONMarkdown

By attaching a route handler to a page or browser, you can inspect each request and decide whether to abort it or let it continue. This guide shows the route-based approach, a Python example, and the decision points that matter when you only want to suppress images or specific URLs.

How request interception works

Playwright exposes a route method on the Page or Browser object. You register an interceptor, and Playwright calls it for matching requests. Inside the handler you can read the request's resource type and URL, then either abort the request or continue it. Aborting stops the resource from loading; continuing allows the normal network flow. This pattern is useful when you want to skip heavy assets such as images or exclude requests whose URL matches a pattern you control.

Python example: block images and URLs containing a keyword

The example below launches Chromium, opens a new page, and registers a handler for every request using the glob pattern "**/*". The handler aborts requests whose resource type is image, aborts requests whose URL contains the keyword, and continues everything else. Replace the placeholder base URL with the site you are working against, and adjust the keyword to match the URLs you want to exclude.

Install Playwright and Chromium and set SITE_BASE_URL to the site origin. This independent example installs interception before requesting the root path, blocks images or URLs containing the case-sensitive text google, and continues every other request. The browser remains visible for 30 seconds; inspect the logged decisions and check whether blocking changes the page.

import os
import time
from playwright.sync_api import sync_playwright

def route_intercept(route):
    request = route.request
    blocked = request.resource_type == "image" or "google" in request.url
    print("ABORT" if blocked else "CONTINUE", request.url)
    if blocked:
        route.abort()
    else:
        route.continue_()

with sync_playwright() as engine:
    browser = engine.chromium.launch(headless=False)
    try:
        page = browser.new_page()
        page.route("**/*", route_intercept)
        page.goto(os.environ["SITE_BASE_URL"].rstrip("/") + "/")
        time.sleep(30)
    finally:
        browser.close()

What the console output looks like

When the handler runs, it prints a line for each blocked request. Image requests are reported with their URL, and URL-matched requests are reported with the keyword that triggered the block. Requests that do not match either condition are continued silently. The output below shows the shape of those messages; the exact URLs depend on the page you load.

Decision checklist: abort or continue?

Use this checklist when writing a route handler:

  • Is the request's resource type one you want to skip (for example, image)? If yes, abort.
  • Does the request URL contain a pattern you want to exclude? If yes, abort.
  • Is the request needed for the page's core content or scripts? If yes, continue.
  • Are you unsure? Continue first, then narrow the abort conditions after observing the output.
  • Does your handler return a value on every branch? Every path should end in abort or continue so Playwright knows what to do.

Field and control map

The handler receives a route object. Two request fields drive the decision:

  • route.request.resource_type — the kind of resource being requested, such as image.
  • route.request.url — the full request URL, which you can search for a keyword.

The two actions are:

  • route.abort() — stop the request.
  • route.continue_() — allow the request to proceed.

Register the handler with page.route("**/*", handler) to cover all requests on the page.

Common failure distinctions

Two opposite mistakes are easy to make. Aborting too broadly can remove scripts or styles the page needs, leaving it broken or empty. Continuing too broadly means your filter never fires, so the resources you meant to block still load. If the page looks incomplete, check whether your abort condition is catching more than intended. If nothing is blocked, check that the handler is registered before navigation and that the URL keyword matches the actual request URLs.

Frequently asked questions

Can I block resources on the Browser object instead of the Page?

The route method is available on both the Page and Browser objects. Registering on the Browser applies the handler more broadly; registering on the Page scopes it to that page. Choose based on how much of the session you want to filter.

What happens to requests that do not match my conditions?

They should be continued. In the example handler, the final branch calls route.continue_(), so any request that is not an image and does not contain the keyword proceeds normally.

Why does my handler need to return on every branch?

Playwright expects the handler to resolve each intercepted request with either an abort or a continue action. If a branch returns nothing, the request may hang or behave unpredictably. Make sure every path ends in route.abort() or route.continue_().