If you can't use async API in jupyter notebook, you can try to create a virtual environment for playwright:
In terminal:
# create a virtual environment for playwright
python3 -m venv playwright_new
source ~/playwright_new/bin/activate
pip install playwright ipykernel requests
playwright install
Then, create a kernel link for jupyter notebook:
source ~/playwright_new/bin/activate
# create kernel link for jupyter notebook
python -m ipykernel install --user --name playwright_new --display-name "playwright_new"
# in mac
ls /Users/xxx/Library/Jupyter/kernels/
tree /Users/xxx/Library/Jupyter/kernels/playwright_new
/Users/xxx/Library/Jupyter/kernels/playwright_new
├── kernel.json
├── logo-32x32.png
└── logo-64x64.png
# or in linux
tree /root/.local/share/jupyter/kernels
Then, run the python code again.
from playwright.async_api import async_playwright
playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless = False)
page = await browser.new_page()
await page.goto("http://whatsmyuseragent.org/")
Error debug
if exec python code throws an Error:
Error: Executable doesn't exist at /Users/xxxx/Library/Caches/ms-playwright/chromium-1000/chrome-mac/Chromium.app/Contents/MacOS/Chromium
╔═════════════════════════════════════════════════════════════════════════╗
║ Looks like Playwright Test or Playwright was just installed or updated. ║
║ Please run the following command to download new browsers: ║
║ ║
║ playwright install ║
║ ║
║ <3 Playwright Team ║
╚═════════════════════════════════════════════════════════════════════════╝
In terminal:
# you already install playwright
playwright install
cd /Users/xxxx/Library/Caches/ms-playwright
ls
chromium-978106/ ffmpeg-1007/ firefox-1319/ webkit-1616/
# but the folder ms-playwright/chromium-1000 NOT EXISTS
# COPY the exists chromium folder with a new name `chromium-1000`
cp -r chromium-978106 chromium-1000