Skip to main content

Use sync mode Playwright interactively, inside a Jupyter notebook

Project description

playwrightnb

This is a Python module that lets you use sync mode Playwright interactively, inside a Jupyter notebook.

To use it:

from playwrightnb import get_page
page = get_page()
page.goto('http://example.org');
# ... do things with `page`...
page.stop()

Unlike non-jupyter usage, you don't use a context manager, but instead use stop when you're done to close the browser session (or to be more precise -- internally we still use a context manager, but we patch it to not auto-close). get_page only returns the page object, not the browser or playwright objects. You should still be able to perform most common web scraping tasks effectively, such as:

  • Navigating to pages using page.goto()
  • Waiting for elements to load with page.wait_for_selector()
  • Extracting data from the page using methods like page.query_selector(), page.query_selector_all(), page.text_content(), etc.
  • Interacting with elements using page.click(), page.type(), page.hover(), and more
  • Evaluating JavaScript with page.evaluate()

However, there are a few capabilities you might miss out on without direct access to the browser or playwright objects:

  1. Launching multiple browser contexts or pages simultaneously
  2. Configuring browser-specific settings or permissions
  3. Accessing browser-level methods like browser.new_page() or browser.close()

For most scraping scenarios, having the page object alone should suffice. But if you need more advanced control, use the following code as a starting point (which is identical to the source of get_page):

with sync_playwright() as p:
    browser = p.chromium.launch(*args, **kw)
    page = browser.new_context().new_page()
    page.stop = p.stop

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playwrightnb-0.1.0.tar.gz (2.5 kB view hashes)

Uploaded Source

Built Distribution

playwrightnb-0.1.0-py3-none-any.whl (2.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page