Browser
Some pages are built by JavaScript after they load, and some sites won’t show anything until a real browser has passed their checks. For those, Netweir drives Chrome. It’s an async API:
import netweir
async with netweir.browser() as browser: page = await browser.new_page() response = await page.goto("https://quotes.toscrape.com/js/") await page.click("li.next a") await page.wait_for("div.quote") root = await page.parse() quotes = root.css("span.text::text").getall()page.parse() gives a Netweir Node, so everything in
Selecting works on the rendered page.
Starting Chrome
Section titled “Starting Chrome”netweir.browser() starts the Chrome at $NETWEIR_CHROME, or else the
newest one netweir install chrome installed, or else the first of Google
Chrome, Chrome for Testing and Chromium in their usual places. An
installed one comes first even when the system’s Chrome is newer, so
after updating Netweir, run netweir install chrome again to move up to
the version its newest profile is for.
If you don’t have Chrome, or want the one Netweir is tuned for:
netweir install chromedownloads the Chrome for Testing build whose version matches Netweir’s
newest Chrome profile into ~/.netweir/chrome/ ($NETWEIR_HOME moves
it). Chrome for Testing is Chrome without the auto-updates, so a crawl’s
cookie hand-back always has a profile that matches. It names itself
Chromium where Google Chrome adds its own name, so its pages present the
brands of the Google Chrome version Netweir has a profile for, as the HTTP
client does. --headless-shell installs chrome-headless-shell instead,
which is lighter and faster but easier to tell from a real browser; pass
its path as executable to use it. A crawl that obeys robots.txt or
TDMRep won’t use it (see In a crawl).
On Windows, the install also lets Chrome’s sandbox read the folder, as
Chrome’s own installer does: Chrome runs its network service and other
helpers in a sandbox that can only read folders granted to app packages,
and without that, pages fail to load with ERR_ABORTED. A Chrome
installed by an earlier Netweir gets the same access the first time it’s
started, so there’s no need to install it again. Netweir never changes
the access of a Chrome it didn’t install.
On Ubuntu 23.10 and later, Chrome for Testing may not start at all: Ubuntu stops the user namespaces its sandbox needs unless an AppArmor profile allows them, and only the Google Chrome package installs one. If Chrome won’t start, the error ends with what Chrome said, such as “No usable sandbox”. Allowing user namespaces keeps the sandbox on:
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0That lasts until the machine restarts. Passing --no-sandbox in args
also works, but leaves pages less contained than Chrome means them to be.
$NETWEIR_CHROME_ARGS adds switches to every Chrome Netweir starts,
separated by spaces, on top of args. It’s for what a machine needs
whatever the code says. On a macOS virtual machine, such as a cloud Mac or
a CI runner, the virtual GPU now and then hangs on Chrome’s work and macOS
stops taking any more from it; Chrome then stops answering, and Netweir’s
error says it “didn’t answer”. Software rendering avoids the GPU:
export NETWEIR_CHROME_ARGS=--disable-gpuA page can tell software rendering from a real GPU through WebGL, so leave it off where the GPU works.
The options:
| Option | Default | Does |
|---|---|---|
executable |
None |
the Chrome to start, instead of searching |
headless |
True |
False opens a window |
args |
() |
more Chrome command-line switches; with your own --user-agent=, pages say that and keep Chrome’s behaviour under the flag: high-entropy client hints empty, and Chrome’s own brands even where Netweir would present Google Chrome’s |
timeout |
30.0 |
the default limit, in seconds, for navigations and actions, and for Chrome to answer anything Netweir asks it |
proxy |
None |
an http://, https:// or socks5:// proxy for everything the browser fetches (Firefox takes socks4:// too); for an HTTP(S) proxy, a username and password in it are given when it asks (neither browser can log in to a SOCKS proxy). Through a SOCKS5 proxy, the proxy looks up every name. |
connect |
None |
drive a browser that’s already running, instead of starting one: a ws:// DevTools URL, or http://host:port |
user_data_dir |
None |
a profile folder to use and keep, so cookies and storage are still there next time |
Use it with async with, or await it and call await browser.close()
yourself. Each browser Netweir starts gets a fresh profile, deleted when it
closes. A program that ends without closing it, even one that crashes or
is killed, still takes Chrome with it within a few seconds. Chrome is
given the time to quit properly first, so a user_data_dir keeps
cookies it hadn’t saved yet. The temporary profile stays behind, as a
netweir-chrome-* folder in the temporary directory, until the next
browser Netweir starts clears it away. A browser you connect to is
never ended this way: Netweir didn’t start it.
To stay logged in from one run to the next, give Chrome a folder of your own:
async with netweir.browser(user_data_dir="~/.cache/shop-profile") as browser: page = await browser.new_page() await page.goto("https://example.com/account")Netweir creates the folder if it isn’t there and never deletes it. The
cookies, local storage and cache a page keeps are saved there when the
browser closes, and the next browser started with the same folder starts
with them. With user_data_dir, new_page() opens pages in the profile
itself, so they share its cookies with each other; new_context() still
gives you a context that’s thrown away. Only one Chrome can use a profile
at a time, so close the first before starting another with the same
folder. Cookies that only last the session (no expiry date) aren’t kept.
Netweir turns off Chrome’s “Preload pages” setting (Settings >
Performance) in the folder before Chrome starts, and it stays off there:
it changes that one value in the Preferences file of the profile Chrome
opens and leaves the rest as it was (see
Blocking and changing requests for
why). Point user_data_dir at the folder you browse with every day and
that setting is off for your own browsing in that profile too; turn it
back on in Chrome’s settings if you want it. A --user-data-dir= switch in
args (or -user-data-dir=, which Chrome reads the same) counts as
user_data_dir: that folder gets the same, and is kept. It’s used as
given, as Chrome uses it: a relative path is relative to the working
directory, and ~ isn’t expanded. A profile name, from
--profile-directory= or the folder’s Local State, has to be one folder
inside it; anything else (.., a path) fails the launch.
Google Chrome won’t let anything drive it with its own default profile,
the folder it opens when started with no --user-data-dir:
~/Library/Application Support/Google/Chrome on macOS,
~/.config/google-chrome on Linux, %LOCALAPPDATA%\Google\Chrome\User Data
on Windows, and the same for Beta, Dev and Canary. Chrome turns remote
control off for that folder, so a launch with it fails at once, saying
so. The way round it is a copy: close Chrome, copy the folder somewhere
else and give Netweir that. The copy keeps settings and local storage,
but Chrome encrypts cookies with a key the copy may not get, so expect
to log in once more. Or use Chrome for Testing, which
netweir install chrome installs and which takes any folder.
Netweir refuses the launch before it touches the folder when it can tell
that’s what you’ve given it; when it can’t, as with a Google Chrome
installed somewhere unusual, Chrome’s own refusal ends the launch.
With connect, Netweir drives a Chrome that’s already running, started
with --remote-debugging-port, or anything else that speaks the DevTools
protocol. Pass the ws:// URL Chrome prints, or http://host:port and
Netweir asks it for the URL; Chrome only answers that when host is an IP
address or localhost, so for one in Docker, say, use the ws:// URL.
Nothing is launched, so executable, args, proxy and headless
don’t apply, and Netweir can’t turn that Chrome’s “Preload pages” setting
off: turn it off yourself, or pages fetched ahead go past your routes. Closing the browser, or dropping it, closes the pages
Netweir opened and leaves the browser running. A wss:// URL works too:
its certificate is checked against Chrome’s own root store (not against
certificates installed on the machine, such as a company’s).
Starting Firefox
Section titled “Starting Firefox”engine="firefox" drives Firefox instead of Chrome:
async with netweir.browser(engine="firefox") as browser: page = await browser.new_page() await page.goto("https://quotes.toscrape.com/js/")It starts the Firefox at $NETWEIR_FIREFOX, or else the newest one
netweir install firefox installed:
netweir install firefoxThat downloads Mozilla’s Firefox 159.0.1 into ~/.netweir/firefox/
($NETWEIR_HOME moves it), checked against the SHA-256 sums Netweir
keeps for that build. The browser tests run on Firefox too, and the
table further down lists what they skip there and why.
Firefox driven over WebDriver BiDi normally tells every page it’s
automated: navigator.webdriver is true and no setting turns that off.
Netweir starts the Firefox it installed so that pages can’t tell. CI
compares what about 45 page-visible properties say in Netweir’s Firefox
and in the same build started by hand, headless and headed, on Linux,
macOS and Windows, and any difference fails the build. To get there,
Netweir adds two startup files to the Firefox it installs, so it won’t
start one it didn’t install that way. To drive any other Firefox, ask
for automation mode, which pages can tell:
netweir.browser(engine="firefox", executable="/Applications/Firefox.app/Contents/MacOS/firefox", stealth=False)browser.engine says which browser is running ("chrome" or
"firefox"), and browser.stealth is False only in automation mode.
A user_data_dir for Firefox must be a new folder, or one an earlier
launch made. Netweir refuses a folder holding a Firefox profile it didn’t
make, stealth=False or not, so it never changes a person’s own.
A kept profile keeps its cookies and storage in the container Netweir’s
pages use, so logins from the profile’s normal tabs aren’t there.
A proxy becomes Firefox’s own proxy setting, so everything Firefox
fetches goes through it, its own requests as well as the pages’. A login
in it is given the way Firefox gives one it saved, without a prompt; one
the proxy refuses fails the page with ERR_PROXY_CONNECTION_FAILED
instead of being tried again. Through a SOCKS5 proxy, Firefox looks up no
names itself. An https:// proxy, which Firefox’s own setting can’t
name, is given to it as a one-line PAC script.
What Firefox doesn’t do, which raises BrowserError naming the feature,
where Chrome would do it:
| Feature | On Firefox |
|---|---|
connect |
Netweir starts its own Firefox; it doesn’t attach to one |
worker_header_order |
Chrome’s only |
a phone’s environment, is_mobile, has_touch |
Chrome’s only |
reduced_motion, forced_colors, contrast and a media type, in an environment or emulate_media() |
Firefox’s WebDriver BiDi overrides only prefers-color-scheme |
color_scheme="no-preference" |
Firefox’s override takes only "light" or "dark" |
| permissions for every origin | Firefox’s permissions.setPermission takes one origin: grant them to an origin instead |
download.cancel() |
Firefox’s WebDriver BiDi has no command to stop a download |
an HTML5 drag (drag() from a draggable element) |
Firefox’s WebDriver starts the drag but ends it without a drop |
start_tracing() and stop_tracing() |
Chrome’s DevTools format |
color_scheme and emulate_media(color_scheme=...) set the same
override Firefox’s DevTools do, so the media query and the colours of a
page drawn in color-scheme: light dark follow it, as they would the
system’s. A cookie with a partition_key is kept under that top-level
site, as one a frame set with Partitioned would be, but cookies()
gives it back without its partition_key: Firefox doesn’t say which
partition a cookie is in.
Keyboard shortcuts edit a field the way the system Firefox runs on does (Command on macOS, Control elsewhere), whatever system the page’s environment says.
Pages and contexts
Section titled “Pages and contexts”browser.new_page() opens a page with cookies and storage of its own, so
two pages never see each other’s logins. For pages that should share them,
open a context:
async with await browser.new_context() as context: first = await context.new_page() second = await context.new_page()Closing a context closes its pages; closing the browser closes everything.
A page or a context can be given an environment: a viewport and screen,
a phone, a language and time zone, a place, permissions and more, with
Playwright’s names (browser.new_context(locale="de-DE", timezone_id="Europe/Berlin"), browser.new_page(device="Galaxy S23")).
A page’s environment has all of it. A page given none
shows a 1280 by 720 viewport on a 1920 by 1080 screen.
Navigating
Section titled “Navigating”await page.goto(url, wait="load") loads the page and waits for its
load event; wait="domcontentloaded" returns sooner, and
wait="networkidle" waits until nothing has been requested for half a
second. It returns a BrowserResponse with the status, final url and
headers of the document. A 404 is returned like any page; a page that
couldn’t be reached at all raises netweir.FetchError, as netweir.get()
does. One failure is tried again before you see it: on Windows 11, Chrome
now and then can’t get a local port for a connection and fails with
net::ERR_NO_BUFFER_SPACE before sending anything. goto() asks again,
up to twice, as a reload would, and raises only if that fails too.
page.url is where the page is now, await page.title() its title and
await page.content() its HTML as Chrome currently has it.
When an action loads another page, await page.wait_for_navigation() waits
for it and returns its response. It waits for a navigation that starts
after it’s called, so start it before the click:
moved = asyncio.ensure_future(page.wait_for_navigation())await page.click("a.next")response = await movedawait page.go_back() and await page.go_forward() move through the
page’s history as the browser’s buttons do, taking the same wait= and
timeout= as goto(). Each returns the response of the document it
lands on, or None when there’s nowhere to go; a new page’s history
starts at the blank page it opened on. Chrome keeps a page you left whole
for a while (its back-forward cache), and going back to it shows it again
without loading it: you get the response it first had, and its scripts
carry on where they were. An entry a script made with history.pushState,
or a #fragment, loads nothing either, so its response has that entry’s
url and a status of 0. await page.reload() loads the page again and
returns its new response.
A page that can’t be loaded again raises netweir.FetchError, as
goto() does: its server has gone, say, or it was the answer to a form
you sent, which Chrome won’t send twice without asking
(net::ERR_CACHE_MISS). If the entry’s answer is a download, or the
page you’re leaving asks whether you really want to go and your dialog
listener says to stay or doesn’t answer, nothing loads, and you get
netweir.BrowserTimeout once the time runs out.
Acting on the page
Section titled “Acting on the page”| Method | Does |
|---|---|
click(selector) |
clicks the middle of the element with the mouse |
dblclick(selector) |
double-clicks it the same way |
fill(selector, text) |
replaces what’s in a field with text in one go, as pasting would |
type(selector, text, delay=None) |
clicks the field and replaces what’s in it by typing text a key at a time, each held for a person’s time, Shift and all; between keys a pause within delay (seconds, or a (least, most) pair; 0.08 to 0.25 by default). See Typing |
press(key) |
presses a key: a character, or "Enter", "Tab", "Escape", "Backspace", "Delete", the arrows, "Home", "End", "PageUp", "PageDown". Hold modifiers with it by naming them first, joined with +: "Shift+Tab", "Control+A", "Meta+Shift+ArrowLeft". See Keyboard shortcuts |
wait_for(selector, state="visible") |
waits until the element is "attached", "visible", "hidden" or "detached" |
hover(selector) |
moves the mouse over the element |
drag(source, target) |
drags one element onto another with the mouse. See Dragging |
scroll(...), select_option(...), check(...), uncheck(...), set_input_files(...) |
see Scrolling, forms and frames |
Selectors are CSS, or XPath after xpath= (page.click("xpath=//button[2]")).
When the markup is a mess of generated class names, say what the element says
instead:
await page.click("text=Sign in") # anywhere in its text, in either caseawait page.click('text="Sign in"') # its whole text, exactlyawait page.click("role=button[name=Sign in]") # a button with that nameawait page.wait_for("role=dialog")text= finds the smallest element holding the text, and reads a button
input’s value too. role= knows an element’s role from its role attribute
or its tag (<a href> is a link, <h2> a heading), and its name from
aria-label, aria-labelledby, a <label>, alt, its text, title or
placeholder, which covers the common cases of the accessible name’s rules
but not all of them. Either picks the first visible match, or the first if
none is visible. Like CSS, neither looks inside shadow roots or other frames;
use a frame for those.
You don’t need to wait before acting: click and
fill keep trying until the element can take the action, up to the
timeout. A click waits for the element to exist, be visible, stop moving,
be enabled and not be covered by anything else; fill waits for a
visible, enabled field that isn’t read-only.
If the time runs out, netweir.BrowserTimeout (a TimeoutError) says
which of those never happened, such as “something else was always at its
centre, covering it”. Each method takes timeout= in seconds.
Keyboard shortcuts
Section titled “Keyboard shortcuts”The modifiers are Shift, Control, Alt and Meta (Command on a
Mac), and ControlOrMeta, which is Meta when the page says it’s on a Mac
and Control anywhere else. That’s the one to use for the shortcuts that
differ, so your script works whatever system your pages say they’re on:
await page.click("#notes")await page.press("ControlOrMeta+A")await page.press("Backspace")Each modifier goes down in the order you name it, a moment apart, then
the key, then they come up in reverse, so the page sees the same
keydown and keyup events, ctrlKey, shiftKey and all, that a
keyboard sends. They come up even if pressing the key fails. Shift with a
digit or symbol types what’s above it on a US keyboard, so
"Shift+1" is !. Modifiers on their own, such as "Shift" or
"Control+Shift", are pressed and let go. "+" on its own is the plus
key, and so is the last + of "Shift++". Names are case-sensitive,
and a modifier or key Netweir doesn’t know raises ValueError before
anything is pressed.
In a text field, the editing shortcuts act as they do on the system the
page says it’s on: select all, deleting and moving a word at a time
(Control off a Mac, Alt on one), to the start or end of the line or
field (Meta and the arrows on a Mac, Control+Home and Control+End
elsewhere), and with Shift held a move selects as it goes. Undo is
ControlOrMeta+Z, and redo is that with Shift, or Control+Y off a
Mac. Copy, cut
and paste don’t work in headless Chrome.
Scrolling, forms and frames
Section titled “Scrolling, forms and frames”Endless feeds
Section titled “Endless feeds”A feed that loads more as you scroll is the commonest reason to reach for a browser at all. Most feeds fetch their items as JSON, so you can skip the HTML and read that:
page = await browser.new_page()await page.goto("https://example.com/feed")
posts = []while True: async with page.expect_response("**/api/posts?*") as info: loaded = await page.scroll(times=1) if not loaded: break # the feed has run out posts += (await (await info.value).json())["posts"]scroll() turns the mouse wheel, as a person would, down to where the
bottom of the page was, then waits up to idle seconds (2 by default) for
the page to grow. With times=1 it does that once and returns 1 if more
loaded, or 0 if nothing did. Each batch the page fetched is the response
expect_response caught on the way.
If you only want the page with everything loaded, await page.scroll()
goes round until nothing more comes and returns how many times something
did; then read it with page.parse(). A feed that never ends never stops
growing, so give it times= to stop after that many rounds.
How long it may take:
- With
times=, that’s the limit, and it isn’t cut short by the browser’stimeout. Each step still has its own bound, so nothing hangs: each wait for more lasts at mostidle, and every request to Chrome the browser’stimeout. - With
timeout=, that’s a limit on the whole call, whatevertimessays. - With neither, the browser’s
timeout(30 seconds unless you changed it) is the limit on the whole call, so a feed still growing after that raisesnetweir.BrowserTimeout. Itsroundsis how many rounds had loaded, and everything that loaded is still in the page, so you can catch it and read on, or read a long feed in rounds withtimes=1as above.
If the API is slow, raise idle; if the page swaps a “Load more” button
in instead, click it.
The wheel turns a notch (100 pixels) at a time, the way a mouse wheel sends it, so a long way down is a long run of small turns. Boxes and frames that scroll by themselves take the wheel before the page does, so each turn goes where nothing like that is under the mouse, out to the edges of the window if need be. One that fills the whole window and won’t pass its scroll on to the page stops the scroll there, as it would stop your own mouse wheel.
scroll() does three other things too:
| Call | Does |
|---|---|
scroll("#reviews") |
wheels the element into view |
scroll(y=600), scroll(x=-200) |
turns the wheel over the middle of the page, by that many pixels (negative is up or left) |
scroll("#feed", y=600) |
turns the wheel over the element, which scrolls it if it scrolls by itself |
Scrolling to the bottom moves the page as a whole. A feed that lives in a
box of its own (the page itself doesn’t scroll) needs
scroll("#feed", y=...) in a loop instead.
Here’s a whole order form: a dropdown, a list where you can pick several, a checkbox and a file.
await page.goto("https://example.com/order")await page.select_option("#size", "m")await page.select_option("#extras", ["milk", "lemon"])await page.select_option("#country", label="United Kingdom")await page.check("#gift-wrap")await page.set_input_files("#receipt", "receipt.pdf")await page.click("button[type=submit]")select_option(selector, value) picks the option with that value
attribute; label= picks by the text shown, and index= by position
from 0. Each takes one or a list, and a list needs a <select multiple>.
It returns the values selected. The page hears input and then change,
as when a person picks something, and hears nothing if the option was
already the one selected. It waits for the <select> to be visible and
enabled and for the option to be there, so options a script adds later
are fine; if the option never turns up, netweir.BrowserTimeout says
which one.
check(selector) and uncheck(selector) click a checkbox or radio
button only if it isn’t already the way you want, then make sure the
click worked. You can point them at a <label> instead, which is handy
when a page hides the real checkbox behind a styled one. A radio button
can’t be unchecked by clicking it, so uncheck on one raises
netweir.BrowserError; check another in its group.
set_input_files(selector, files) takes a path or a list of paths, and a
list needs <input type=file multiple>. A missing file raises
FileNotFoundError before anything is sent. An empty list clears the
input. With connect, the paths are read on the machine Chrome runs on.
hover(selector) moves the mouse over the element, for menus and
tooltips that only appear on :hover or mouseover.
Dragging
Section titled “Dragging”drag(source, target) presses the mouse on the middle of source, moves
it a step at a time to the middle of target and lets go:
await page.drag("#card-7", "#column-done")That works for both kinds of drag a page can have. Sliders, sortable
lists and map panning follow the mouse itself, and see mousedown, a
run of mousemove and mouseup. An element with draggable=true makes
an HTML5 drag and drop instead, and the page sees dragstart,
dragenter, dragover, drop and dragend, with whatever the page put
in dataTransfer there to read on the drop. source must be visible,
still and not covered, as for a click; target must be visible and
still. Both have to show in the window at once, since a held mouse can’t
scroll the page: if they don’t, drag raises ValueError before
pressing anything, and you can scroll or make the window bigger. If a
drag fails partway, it’s called off and the button let go, so the page
isn’t left mid-drag. A phone’s page has no mouse, so drag raises
ValueError there too.
Frames
Section titled “Frames”Selectors only look inside the document they run in, so an element in an
<iframe> can’t be reached from the page. Get the frame and act on that:
frame = await page.frame(name="checkout")await frame.fill("#card-name", "Ada Lovelace")await frame.click("button.pay")
for frame in await page.frames(): print(frame.name, frame.url)page.frames() lists every frame, the page’s own first. page.frame()
finds the first with that name, a url matching a glob or compiled
regex, or both, and gives None if there isn’t one. A frame has the
methods a page uses to read and act: content, parse, title,
evaluate, click, fill, hover, drag, scroll, select_option,
check, uncheck, set_input_files and wait_for. Keys go wherever focus is,
so after frame.fill(...), page.press("Enter") presses Enter in the
frame. Frames from other sites, which Chrome runs in processes of their
own, work the same way.
A frame’s name and url are what they were when you asked for it.
Once the frame loads a different page, from this site or another (a
payment or sign-in frame often does), its actions work on the new one;
once the frame is removed they raise netweir.BrowserError.
A frame’s requests count as the page’s, whichever site it’s from.
on_request, on_response and the wait_for_/expect_ methods hear
them, block() and route() apply to them, and a response’s body()
works the same. That includes a route you add after the frame has
loaded. So if a payment frame from another site fetches its JSON, you
can still wait for it:
async with page.expect_response("**/api/quote*") as info: await frame.click("#refresh")quote = await (await info.value).json()Dialogs, popups and downloads
Section titled “Dialogs, popups and downloads”Dialogs
Section titled “Dialogs”An alert(), confirm() or prompt() stops the page until someone
answers it, and so does the “leave this site?” question a page can ask
before it unloads. Netweir answers every one, so a page never waits for
nobody. Alerts, confirms and prompts are dismissed, as pressing Escape
would. A page asking before it unloads is left: your own goto() is
what asked to leave.
To answer them yourself, add a listener:
async def answer(dialog): if dialog.type == "prompt": await dialog.accept("Ada") else: await dialog.dismiss()
page.on_dialog(answer)A dialog has type ("alert", "confirm", "prompt" or
"beforeunload"), message, default_value (the text a prompt offers)
and url, the frame that opened it. accept(prompt_text=None) presses
OK, with prompt_text in a prompt’s box; dismiss() presses Cancel.
A listener can be an async function, or a plain one, which can call
accept() or dismiss() without awaiting it. One that returns without
answering, or raises, leaves the dialog to the default, and answering
twice raises netweir.BrowserError. Dialogs opened by frames, other
sites’ included, come to the page’s listeners.
page.remove_dialog_listener(answer) goes back to the defaults.
A dialog’s listeners run one at a time on the event loop that added the first of them, and a page’s dialogs are meant for one loop at a time, as its other listeners are.
Whoever answers, the answer reaches the page between half a second and
a second and a half after the dialog opened, picked at random each time.
A page can time how long confirm() kept it waiting, and nobody answers
in a millisecond. So each dialog costs you about a second. To change
that, launch with netweir.browser(dialog_pause=(least, most)) in
seconds; the same pause comes before using a tab brought back to the
front (see below). Shorter is faster, and easier for a page to spot.
close() never waits on a page’s question about leaving: it closes the
page without asking.
Popups and new tabs
Section titled “Popups and new tabs”A link with target=_blank, or a script that calls window.open() when
you click, opens a new page. Catch it the way you catch a response:
async with page.expect_popup() as info: await page.click("a.details")popup = await info.valueawait popup.wait_for_load()details = await popup.parse()await page.wait_for_popup() waits from when it’s called, and
page.on_popup(callback) hears every one. A popup is a BrowserPage
like any other. It reaches you once it’s running, before its document
has loaded, which is what await popup.wait_for_load() waits for; it
returns that document’s response. wait_for_load() takes wait= and
timeout= as goto() does, and works on any page: it waits for the
document showing now, or one that replaced it.
What you set up on the opener reaches the popup from its first request.
Its route() and block() apply there, behind any routes the popup
gets of its own, and so do its context’s; the popup shares its
opener’s cookies, and looks no more automated than any other page.
Listeners don’t carry over: add them to the popup. Closing a page from
browser.new_page() closes its popups too.
A new tab opens in front, and Chrome counts the tab it was opened from
as hidden, as in a window you’re looking at. When you act on the opener
again (click, fill, press, hover, drag, scroll, check,
select_option, set_input_files), or wait on it (wait_for,
wait_for_navigation, wait_for_load), Netweir brings its tab back to
the front first, and pauses a moment, as you would.
Firefox puts every page in a window of its own, so every page is showing, but only one window has the focus at a time. Acting on a page gives it the focus first, with the same pause. Waiting on one doesn’t need the focus, only a page that’s showing, so pages you wait on side by side don’t take turns at it, and don’t pause.
Reading it doesn’t: evaluate, content, parse, title and
screenshot work on a tab behind and leave it there, so reading a
popup and its opener in turn doesn’t flip between them. A tab behind
gets no animation frames and slow timers, though, so a script you
evaluate there that waits on the page’s next frame
(requestAnimationFrame) won’t finish until the tab is in front again.
Chrome blocks a popup that isn’t opened from a click, for Netweir as for
anyone, so page.evaluate("window.open('/x')") opens nothing. Click
whatever opens it.
Downloads
Section titled “Downloads”A link to a file, or a response sent as an attachment, downloads instead of showing:
async with page.expect_download("**/*.csv") as info: await page.click("a.export")download = await info.valueawait download.save_as("prices.csv")A download has url and suggested_filename, the name the site gave
it. await download.save_as(path) copies the file once it has all
arrived, await download.body() gives its bytes, and
await download.path() gives the file itself. They wait for the
download to finish, and raise netweir.BrowserError if it didn’t.
await download.failure() waits too, and gives None, or "canceled":
that’s what Chrome reports for a download that was cancelled and for one
whose connection broke. await download.cancel() stops one.
Like every other wait, none of them waits longer than the browser’s
timeout. A big file on a slow line can need more, so each takes
timeout= in seconds: save_as(path, timeout=600), say. Running out
raises netweir.BrowserTimeout and leaves the download going, so cancel
it if you’ve given up on it.
wait_for_download and expect_download take a pattern as
wait_for_response does, or none for the next download, and
on_download(callback) hears each one, frames’ downloads included,
other sites’ too.
Each context saves its downloads in a temporary folder of its own,
removed when the context closes. A page from browser.new_page() has a
context of its own, so its downloads go when the page closes: save what
you want to keep. Nothing lands in your Downloads folder. On Windows, a
download still under way when its context closes can keep its folder
there until the browser closes, since Windows won’t delete a file Chrome
has open.
Nobody listening, nothing downloads. A download that reaches no
on_download, wait_for_download or expect_download is cancelled as
it starts, so a link to a 4 GB file can’t fill your disk behind your
back, and so is one you let go of before it finishes. Keep hold of the
download (or of save_as() running on it) for as long as you want it.
goto() to a file raises netweir.FetchError with net::ERR_ABORTED,
since no page came back. With a download listener, the download happens
all the same, and on_download hears it.
With connect=, the folder is on the machine running Netweir, so a
Chrome running somewhere else (in Docker, say) can’t save there.
Running JavaScript
Section titled “Running JavaScript”count = await page.evaluate("document.querySelectorAll('li').length")data = await page.evaluate("() => fetch('/api/items').then(r => r.json())")evaluate runs an expression, or a function, which it calls. A promise
is awaited, and the result comes back as Python values (dicts, lists,
strings, numbers, True, False, None). Values JSON can’t hold come
back as text: NaN, Infinity, -0 and BigInts as "NaN",
"Infinity", "-0" and their digits. An error thrown in the page raises
netweir.BrowserError with its message.
evaluate has no timeout of its own, so a promise that never settles
waits forever. Wrap it in asyncio.wait_for if that can happen.
Screenshots, PDFs and cookies
Section titled “Screenshots, PDFs and cookies”await page.screenshot("page.png", full_page=True) saves a PNG and returns
its bytes; leave out the path to get only the bytes. quality= (1 to 100)
makes a JPEG of that quality instead, often a tenth the size.
await page.pdf("page.pdf") saves the page laid out as for printing and
returns the bytes; leave out the path to get only the bytes. Printing drops
background colours and images unless you pass print_background=True.
await page.pdf( "report.pdf", format="A4", # Letter, Legal, Tabloid, A3, A4 or A5; Letter by default landscape=True, margin=0.5, # inches, on all four sides scale=0.8, page_ranges="1-3, 5",)width= and height=, in inches, take the place of format=. landscape
swaps the two, so give them as they’d be upright.
await page.cookies() returns the cookies of the page’s context as dicts
with name, value, domain, path, expires, http_only, secure,
same_site and partition_key. await page.set_cookies([...]) takes the
same dicts, or Playwright’s (httpOnly, sameSite); name, value and
domain are required. A domain with a leading dot is the domain’s and its
subdomains’; without one, the cookie stays that host’s alone, where
Playwright’s add_cookies would make it the domain’s.
await context.storage_state(path="state.json") saves the context’s
cookies and localStorage in Playwright’s format, and
browser.new_context(storage_state="state.json") starts a context with
them. The same file starts a netweir.Client logged in. See
Sessions and cookies.
Tracing
Section titled “Tracing”When a page is slow and you can’t tell why, record what Chrome did while it loaded:
await browser.start_tracing(screenshots=True)await page.goto("https://example.com/")await browser.stop_tracing("trace.json")Open trace.json in the Performance panel of Chrome’s developer tools, or at
ui.perfetto.dev. stop_tracing returns the bytes
too, and without a path that’s all it does.
A trace covers the whole browser, every page in it, and a browser records one
at a time: starting a second raises an error, as does stopping when none has
started. categories=
takes Chrome’s trace categories in place of the ones the Performance panel
records. Tracing slows Chrome down and a trace can run to many megabytes, so
it’s for looking into a problem, not for leaving on in a crawl. It holds the
URLs the pages visited, and with screenshots=True what they showed, so treat
the file as you would the pages themselves.
Reading what the page fetched
Section titled “Reading what the page fetched”Plenty of pages that need JavaScript are a thin shell around an API. The list you’re after arrives as JSON, and the page’s script turns it into HTML. You can skip the HTML and take the JSON:
page = await browser.new_page()await page.goto("https://example.com/catalogue")async with page.expect_response("**/api/products*") as info: await page.click("button.load-more")response = await info.valueproducts = (await response.json())["products"]expect_response starts waiting when the async with block starts, so
the response can’t arrive before you’re listening, however fast the
server is. await info.value is the first response that matched. If you
just want to wait, await page.wait_for_response(pattern) does the same
from the moment it’s called.
A pattern is one of three things:
- A glob, matched against the whole URL:
*is anything but a/,**is anything at all,{png,jpg}is either, and every other character,?included, is itself. So"**/api/*"matcheshttps://example.com/api/items?page=2, and"**/*.{png,jpg}"matches every PNG and JPEG. - A compiled regex, searched for in the URL:
re.compile(r"/api/v\d+/"). - A function that takes the response and returns true for the one you
want:
lambda r: r.resource_type == "xhr" and r.status == 200. It can beasync; a slow one holds nothing else up, but amongasyncfunctions the first to say yes wins, which may not be the earliest response.
A response has url, status, headers, method (the request’s) and
resource_type: "document", "xhr", "fetch", "image",
"stylesheet", "script", "font", "media" and so on. Its body is
fetched only when you ask: await response.body() for bytes,
await response.text() for text read as UTF-8, await response.json()
for the parsed JSON. They wait for the body to finish arriving. The
response goto returns works the same way.
Chrome keeps bodies only for the page that’s showing. Once the page moves
on to another document, the bodies from the old one are gone, and asking
for one raises netweir.BrowserError saying so. Read what you need
before you navigate. A body Chrome couldn’t decode, such as a broken
image, comes back empty.
To see everything rather than wait for one response, add a listener:
def seen(response): print(response.status, response.resource_type, response.url)
page.on_response(seen)await page.goto("https://example.com/")page.remove_response_listener(seen)A listener can be a plain function or an async one, and an exception in
it is logged to the netweir logger rather than raised. on_request,
wait_for_request and expect_request do the same for requests as they
go out; a request has url, method, headers, resource_type and
post_data (the body it sends, as bytes, or None).
Nothing piles up behind your back. Netweir only hears Chrome’s network events while a listener or a wait wants them, and keeps none of them: a response nobody asked for is let go as it arrives.
wait_for_response and expect_response take timeout= in seconds, the
browser’s timeout by default, and raise netweir.BrowserTimeout when
it runs out. If the page closes first, they raise netweir.BrowserError.
Use a page’s listeners, waits and routes from one event loop at a time.
A page can outlive its loop (two asyncio.run calls, or a test framework
that gives each test a loop of its own). When the loop that reads the
page’s events or routes ends, every listener and route on the page is
dropped, whichever loop added it, and the page carries on without them:
its requests go on unchanged. Add what you need again in the new loop.
A listener or wait added from another thread’s loop does run on that
loop, but its routes would still be served by the first.
Blocking and changing requests
Section titled “Blocking and changing requests”The quickest win is not fetching what you’ll never look at. Images, fonts and video are most of a page’s weight:
page = await browser.new_page()await page.block() # images, fonts and mediaawait page.block(resource_types=(), urls=["**/analytics/**", "**/*.mp4"])await page.goto("https://example.com/")block() takes resource_types (("image", "font", "media") unless you
say otherwise) and urls, a list of globs. Each call adds to what the
page already blocks. A blocked request fails inside Chrome before it’s
sent, so the server never sees it, and the page sees a network error as
it would for any failed request. Before a request is sent, Chrome can’t
tell fetch() from XMLHttpRequest, so blocking "xhr" or "fetch"
blocks both.
For more than yes or no, add a route. Every request whose URL matches the pattern goes to your handler, which decides what happens to it:
async def handle(route): if route.request.resource_type == "xhr": await route.fulfill(json={"items": ["a test item"]}) else: headers = dict(route.request.headers) | {"X-Debug": "1"} await route.continue_(headers=headers)
await page.route("**/api/**", handle)The handler gets a Route, with the request as route.request, and calls
one of:
| Method | Does |
|---|---|
abort() |
fails the request; the page sees a network error |
continue_(url=None, method=None, headers=None, body=None) |
sends it on, changed as asked; headers replace all of the request’s headers, so start from route.request.headers to change one |
fulfill(status=200, headers=None, body=None, json=None) |
answers it without it leaving Chrome; body is bytes or a str, and json is sent as JSON with an application/json content type |
Patterns work as they do for waiting, except that a function gets the
URL rather than a response. Routes are asked in turn, so a slow async
pattern function holds up the page’s other routed requests. When more
than one route matches, the newest wins. await page.unroute(pattern) removes the routes with that pattern
(only the one with that handler, if you pass it as well).
Replaying a HAR file
Section titled “Replaying a HAR file”A HAR file holds what a page fetched and what came back. Replay one and the page gets those answers with nothing sent to the network, so you can run a scraper against a site’s recorded pages as often as you like, offline, and get the same result each time:
await page.route_from_har("shop.har")await page.goto("https://shop.example/")Save the file from Chrome’s or Firefox’s network panel, or use the one a
crawl’s har setting writes. A
request is matched on its method and URL, and on its body when the same URL
was asked different things. Requests alike get the answers in the order they
were recorded, and then the last one again.
A request the file doesn’t hold fails, as if the network were down, so nothing
leaks out by accident. Pass not_found="fallback" to let those go to the
network instead, and url= (a pattern, as for route) to replay only part of
a site. route_from_har is a route like any other: a newer route wins over it,
and it works on a context too.
A context has route and unroute too, and they apply to every page in
it, the ones open now and the ones you open later. A page’s own routes
come first; if none of them matches, the context’s get the request.
Requests are never left hanging. A handler that returns without deciding,
or raises (the exception goes to the netweir logger), lets the request
go on unchanged. One that hasn’t decided by the browser’s timeout (30
seconds unless you changed it) loses the request, which goes on unchanged
too; deciding after that raises netweir.BrowserError.
Routing has a cost: while a page has routes, every request it makes is
held in Chrome until Netweir has asked your Python code about it, which is
a round trip each. block() with resource types only holds requests of
those types, and decides them without asking Python.
A page can ask Chrome to fetch pages before anyone follows a link to
them, with speculation rules (<script type="speculationrules">) or
<link rel=prerender>. Chrome would do that fetching itself, where no
route sees it, so every Chrome Netweir starts has preloading turned off,
as you can turn it off in Chrome’s settings; it fetches none of them, and
a page can’t tell. Following such a link is then an ordinary navigation
that your routes see. <link rel=prefetch> is the page’s own request,
routed like an image.
Some things routes don’t see. In a crawl, a page that robots.txt or
TDMRep rules out is refused before any route is asked, and no route can
answer for it. Nor can a route send a page elsewhere to get round them:
continue_(url=...) on a page the crawl checked has the new URL checked
too, and one ruled out fails with ERR_BLOCKED_BY_CLIENT, exactly as a
refused page does, without being fetched. Requests from frames, other
sites’ included, and from the page’s workers are heard and routed like
the page’s own. Known limit: in a browser you connect to with
preloading on, or in chrome-headless-shell, which ignores the setting,
what a page’s speculation rules ask Chrome to fetch ahead goes out
unrouted. And
if you pass --disable-features=NoStatePrefetchHoldback,
<link rel=prerender> is fetched unrouted again. What service workers and shared workers fetch for
themselves isn’t: Chrome runs those for the browser, not the page. Nor
is the script of a worker started by another worker, though what that
worker fetches afterwards is. A worklet’s module (audioWorklet,
CSS.paintWorklet) is routed and blocked, but listeners don’t hear it. A redirect’s own response
isn’t reported to listeners: you hear about the response the redirects
end at.
In a crawl
Section titled “In a crawl”A spider can send some requests through Chrome and the rest through the much faster HTTP client:
class Shop(netweir.Spider): async def start(self): yield netweir.Request("https://example.com/catalogue", browser=True)
async def parse(self, page): await page.browser.click("button.load-more") await page.browser.wait_for("li.product:nth-child(40)") root = await page.browser.parse() for href in root.css("li.product a::attr(href)").getall(): yield page.follow(href)The page a browser request gives its callback is the one Chrome rendered,
so page.css(...) sees what the scripts built. page.browser is the live
page until the callback returns, then it closes. Browser requests wait their
turn like any other: robots.txt, each site’s limits and its delay all
apply. Chrome follows redirects, and a page’s own scripts, by itself; each
page it’s about to load in the tab is checked against that site’s
robots.txt and TDMRep first, and one they rule out isn’t fetched (the
request is dropped, as over HTTP). Chrome fetches no page ahead of time
for the crawl, so a page’s speculation rules can’t get one past the
check either. chrome-headless-shell would, whatever its settings say,
so a crawl with obey_robots or obey_tdmrep on refuses to start it:
its browser requests fail with an error saying so. Use Chrome
(netweir install chrome, without --headless-shell), or turn both
off if you mean to crawl without them. The check stays on while your callback
drives page.browser: navigating it somewhere ruled out fails with
Chrome’s ERR_BLOCKED_BY_CLIENT. browser_pages (4 by default) caps how many are
open at once. browser_block=("image", "font", "media") has every page
the crawl opens in Chrome skip those, as page.block() would.
A declarative spider’s rules read the rendered HTML, so callbacks the rules
call, and a rules spider’s own parse, get the rendered page but not
page.browser.
For what most pages need before they’re read, such as a banner to
dismiss, a button to click or a feed to scroll, a request can carry
steps, which Chrome does before the callback, and a rule can carry them
too. See Steps.
A page Chrome answers with a server error (500, 502, 503, 504, 408, 522 or
524) is tried again in Chrome, up to retries times, as the HTTP client
would; other error statuses go to the callback as they are.
Two settings make Chrome step in on its own:
browser="on_block": a request still blocked after its retries is loaded once in Chrome, which waits up to 20 seconds for a challenge to pass. If it gets through, the site’s HTTP session takes Chrome’s cookies, so the next requests to that site go back to the HTTP client. Those cookies are often tied to the browser that earned them, so the session switches to Netweir’s profile for the installed Chrome’s version; if Netweir has none, a warning says so. If Chrome can’t be started, the request goes toon_blockas before, and a warning says why.browser="always": every request goes through Chrome.
browser_engine="firefox" has the crawl use Firefox for all of this
instead, the one netweir install firefox installed. Cookies it earns go
back to the HTTP client with Netweir’s profile for that Firefox version,
as Chrome’s do with Chrome’s. browser_engine="match" picks the browser
the crawl’s requests already look like: Firefox when profile is a
Firefox one, Chrome otherwise. Firefox goes through the crawl’s proxy as
Chrome does.
Chrome is started the first time the crawl needs it, and again if it dies. If it can’t start, browser requests fail, each saying why, and it’s tried again a minute later.
Chrome goes through the crawl’s proxy, login included, so the cookies it
earns come from the address the HTTP client uses. It doesn’t move through
proxies as a blocked HTTP session does: it and the session it hands its
cookies to both use proxy. With proxy_rotation="per_request" or
"per_host" there’s no proxy, so Chrome takes the next healthy one of
proxies when it starts and keeps it for every page; the session it hands
its cookies to goes through that one too.
The stats count browser_fetches, pages Chrome fetched, and
browser_unblocked, blocked requests it got through. Each hand-back counts
in sessions_replaced too. browser_steps and browser_steps_failed
count the steps run on those pages, and the ones that failed.
Not looking automated
Section titled “Not looking automated”A browser that’s being driven usually shows it, and bot protection looks. Netweir avoids these giveaways:
navigator.webdriveris false.- Headless Chrome calls itself
HeadlessChromein its user agent. Netweir sends the normal user agent instead, with the client hints Chrome reports for itself, in the request headers, in the page, and in its workers and service workers. Chrome only says what those are before the user agent is changed, so the first headless launch on a machine starts a Chrome of its own to ask. The answer is kept in~/.netweir/chrome-identity.jsonfor the next program, and asked again when Chrome or the operating system is updated. - It never turns on the DevTools domains whose side effects page scripts can notice, and its own scripts run where the page can’t see them.
- Clicks, keys, hovering and scrolling are real input events, not JavaScript calls.
- A dialog is never answered sooner than a person could answer it, and Chrome’s popup blocker stays on.
- Headless Chrome’s own screen is 800 by 600, smaller than the windows it opens. Netweir gives it a 1920 by 1080 one, and sizes every page’s real window, so the window always fits the screen.
- A viewport, a screen, a phone, a language or a time zone is set where Chrome sets it for itself, so the page, its workers and frames from other sites all say the same; see A page’s environment.
- Chrome for Testing is started as Google Chrome ships: without the infobar that took 56 pixels from every page’s window, and without the field trial config that showed page script one language and moved Accept-Language among a document’s headers.
- With
netweir.browser(worker_header_order=True), a worker’s script is asked for with its headers in Google Chrome’s order, which DevTools’ user agent override otherwise changes (User-Agent comes early). It’s off by default because it slows every page; see A page’s environment.
A test in the repository loads a page that checks the first two from page script and the server checks the request headers; the test also fails if any of the DevTools domains in the third was turned on.
That isn’t everything a site can check. select_option, and clearing a
file input, change the element from a script and send the input and
change events it would send; a page that checks event.isTrusted can
tell those apart from a person’s. In a service worker, the
high-entropy client hints (getHighEntropyValues) come back empty, and
headless Chrome on a Linux server without a GPU reports a software renderer
to WebGL. Pass headless=False where that matters.