A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://playwright.dev/python/docs/api/class-route below:

Route | Playwright Python

Route

Whenever a network route is set up with page.route() or browser_context.route(), the Route object allows to handle the route.

Learn more about networking.

Methods abortAdded before v1.9 route.abort

Aborts the route's request.

Usage

route.abort()
route.abort(**kwargs)

Arguments

Returns

continue_Added before v1.9 route.continue_

Sends route's request to the network with optional overrides.

Usage

def handle(route, request):

headers = {
**request.headers,
"foo": "foo-value",
"bar": None
}
route.continue_(headers=headers)

page.route("**/*", handle)
async def handle(route, request):

headers = {
**request.headers,
"foo": "foo-value",
"bar": None
}
await route.continue_(headers=headers)

await page.route("**/*", handle)

Arguments

Returns

Details

The headers option applies to both the routed request and any redirects it initiates. However, url, method, and post_data only apply to the original request and are not carried over to redirected requests.

route.continue_() will immediately send the request to the network, other matching handlers won't be invoked. Use route.fallback() If you want next matching handler in the chain to be invoked.

warning

The Cookie header cannot be overridden using this method. If a value is provided, it will be ignored, and the cookie will be loaded from the browser's cookie store. To set custom cookies, use browser_context.add_cookies().

fallbackAdded in: v1.23 route.fallback

Continues route's request with optional overrides. The method is similar to route.continue_() with the difference that other matching handlers will be invoked before sending the request.

Usage

When several routes match the given pattern, they run in the order opposite to their registration. That way the last registered route can always override all the previous ones. In the example below, request will be handled by the bottom-most handler first, then it'll fall back to the previous one and in the end will be aborted by the first registered route.

page.route("**/*", lambda route: route.abort())  
page.route("**/*", lambda route: route.fallback())
page.route("**/*", lambda route: route.fallback())
await page.route("**/*", lambda route: route.abort())  
await page.route("**/*", lambda route: route.fallback())
await page.route("**/*", lambda route: route.fallback())

Registering multiple routes is useful when you want separate handlers to handle different kinds of requests, for example API calls vs page resources or GET requests vs POST requests as in the example below.


def handle_get(route):
if route.request.method != "GET":
route.fallback()
return




def handle_post(route):
if route.request.method != "POST":
route.fallback()
return



page.route("**/*", handle_get)
page.route("**/*", handle_post)

async def handle_get(route):
if route.request.method != "GET":
await route.fallback()
return




async def handle_post(route):
if route.request.method != "POST":
await route.fallback()
return



await page.route("**/*", handle_get)
await page.route("**/*", handle_post)

One can also modify request while falling back to the subsequent handler, that way intermediate route handler can modify url, method, headers and postData of the request.

def handle(route, request):

headers = {
**request.headers,
"foo": "foo-value",
"bar": None
}
route.fallback(headers=headers)

page.route("**/*", handle)
async def handle(route, request):

headers = {
**request.headers,
"foo": "foo-value",
"bar": None
}
await route.fallback(headers=headers)

await page.route("**/*", handle)

Use route.continue_() to immediately send the request to the network, other matching handlers won't be invoked in that case.

Arguments

Returns

fetchAdded in: v1.29 route.fetch

Performs the request and fetches result without fulfilling it, so that the response could be modified and then fulfilled.

Usage

def handle(route):
response = route.fetch()
json = response.json()
json["message"]["big_red_dog"] = []
route.fulfill(response=response, json=json)

page.route("https://dog.ceo/api/breeds/list/all", handle)
async def handle(route):
response = await route.fetch()
json = await response.json()
json["message"]["big_red_dog"] = []
await route.fulfill(response=response, json=json)

await page.route("https://dog.ceo/api/breeds/list/all", handle)

Arguments

Returns

Details

Note that headers option will apply to the fetched request as well as any redirects initiated by it. If you want to only apply headers to the original request, but not to redirects, look into route.continue_() instead.

fulfillAdded before v1.9 route.fulfill

Fulfills route's request with given response.

Usage

An example of fulfilling all requests with 404 responses:

page.route("**/*", lambda route: route.fulfill(
status=404,
content_type="text/plain",
body="not found!"))
await page.route("**/*", lambda route: route.fulfill(
status=404,
content_type="text/plain",
body="not found!"))

An example of serving static file:

page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json"))
await page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json"))

Arguments

Returns

Properties requestAdded before v1.9 route.request

A request to be routed.

Usage

Returns


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4