Method: Playwright::Page#route

Defined in:
lib/playwright_api/page.rb

#route(url, handler) ⇒ Object

Routing provides the capability to modify network requests that are made by a page.

Once routing is enabled, every request matching the url pattern will stall unless it’s continued, fulfilled or aborted.

> NOTE: The handler will only be called for the first url if the response is a redirect.

An example of a naïve handler that aborts all image requests:

“‘js const page = await browser.newPage(); await page.route(’*/.png,jpg,jpeg‘, route => route.abort()); await page.goto(’example.com’); await browser.close(); “‘

“‘python async page = await browser.new_page() await page.route(“*/.png,jpg,jpeg”, lambda route: route.abort()) await page.goto(“example.com”) await browser.close() “`

“‘python sync page = browser.new_page() page.route(“*/.png,jpg,jpeg”, lambda route: route.abort()) page.goto(“example.com”) browser.close() “`

or the same snippet using a regex pattern instead:

“‘js const page = await browser.newPage(); await page.route(/(.png$)|(.jpg$)/, route => route.abort()); await page.goto(’example.com’); await browser.close(); “‘

“‘python async page = await browser.new_page() await page.route(re.compile(r“(.png$)|(.jpg$)”), lambda route: route.abort()) await page.goto(“example.com”) await browser.close() “`

“‘python sync page = browser.new_page() page.route(re.compile(r“(.png$)|(.jpg$)”), lambda route: route.abort()) page.goto(“example.com”) browser.close() “`

Page routes take precedence over browser context routes (set up with [‘method: BrowserContext.route`]) when request matches both handlers.

> NOTE: Enabling routing disables http cache.



1240
1241
1242
# File 'lib/playwright_api/page.rb', line 1240

def route(url, handler)
  wrap_impl(@impl.route(unwrap_impl(url), unwrap_impl(handler)))
end