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.abortAborts the route's request.
Usage
route.abort()
route.abort(**kwargs)
Arguments
Optional error code. Defaults to failed
, could be one of the following:
'aborted'
- An operation was aborted (due to user action)'accessdenied'
- Permission to access a resource, other than the network, was denied'addressunreachable'
- The IP address is unreachable. This usually means that there is no route to the specified host or network.'blockedbyclient'
- The client chose to block the request.'blockedbyresponse'
- The request failed because the response was delivered along with requirements which are not met ('X-Frame-Options' and 'Content-Security-Policy' ancestor checks, for instance).'connectionaborted'
- A connection timed out as a result of not receiving an ACK for data sent.'connectionclosed'
- A connection was closed (corresponding to a TCP FIN).'connectionfailed'
- A connection attempt failed.'connectionrefused'
- A connection attempt was refused.'connectionreset'
- A connection was reset (corresponding to a TCP RST).'internetdisconnected'
- The Internet connection has been lost.'namenotresolved'
- The host name could not be resolved.'timedout'
- An operation timed out.'failed'
- A generic failure occurred.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
headers
Dict[str, str] (optional)#
If set changes the request HTTP headers. Header values will be converted to a string.
If set changes the request method (e.g. GET or POST).
post_data
str | bytes | Dict (optional)#
If set changes the post data of request.
If set changes the request URL. New URL must have same protocol as original one.
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().
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
headers
Dict[str, str] (optional)#
If set changes the request HTTP headers. Header values will be converted to a string.
If set changes the request method (e.g. GET or POST).
post_data
str | bytes | Dict (optional)#
If set changes the post data of request.
If set changes the request URL. New URL must have same protocol as original one. Changing the URL won't affect the route matching, all the routes are matched using the original request URL.
Returns
fetchAdded in: v1.29 route.fetchPerforms 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
headers
Dict[str, str] (optional)#
If set changes the request HTTP headers. Header values will be converted to a string.
max_redirects
int (optional) Added in: v1.31#
Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20
. Pass 0
to not follow redirects.
max_retries
int (optional) Added in: v1.46#
Maximum number of times network errors should be retried. Currently only ECONNRESET
error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0
- no retries.
If set changes the request method (e.g. GET or POST).
post_data
str | bytes | Dict (optional)#
Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type
header will be set to application/json
if not explicitly set. Otherwise the content-type
header will be set to application/octet-stream
if not explicitly set.
timeout
float (optional) Added in: v1.33#
Request timeout in milliseconds. Defaults to 30000
(30 seconds). Pass 0
to disable timeout.
If set changes the request URL. New URL must have same protocol as original one.
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.fulfillFulfills 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
Response body.
If set, equals to setting Content-Type
response header.
headers
Dict[str, str] (optional)#
Response headers. Header values will be converted to a string.
json
Dict (optional) Added in: v1.29#
JSON response. This method will set the content type to application/json
if not set.
path
Union[str, pathlib.Path] (optional)#
File path to respond with. The content type will be inferred from file extension. If path
is a relative path, then it is resolved relative to the current working directory.
response
APIResponse (optional) Added in: v1.15#
APIResponse to fulfill route's request with. Individual fields of the response (such as headers) can be overridden using fulfill options.
Response status code, defaults to 200
.
Returns
Properties requestAdded before v1.9 route.requestA 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