playwright在vscode+jupyter中出现NotImplementedError问题

近期因个人需要接触playwright,由于playwright新接触,想用jupyter进行API测试学习。刚开始使用sync_playwright,在playwright的Conda运行环境中,以console模式和单文件直接运行模式,都能正常运行。但是进入jupyter中后,sync_playwright就无法运行了。会出现如下提示:

Error: It looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.

在网上找到(如何在 Jupyter 笔记本中使用 Playwright 库而不是使用常规 .py 脚本(在 Windows 上) - python - SO中文参考 - www.soinside.com)这篇文章,才知道"Jupyter Notebook 使用 asyncio 事件循环,因此您应该使用 async api"。

于是将sync_playwright改成 async_playwright。然后又面临上述文章中的同样问题。

Not Implemented error

按照上述文章所说,更改jupyter核心运行库:

1、导航到 Python 目录中的 Lib/site-packages/ipykernel/kernelapp.py

2、将该文件中的一行代码注释掉

if sys.platform.startswith("win") and sys.version_info >= (3, 8):
    import asyncio

    try:
        from asyncio import WindowsProactorEventLoopPolicy, WindowsSelectorEventLoopPolicy
    except ImportError:
        pass
        # not affected
    else:
        if type(asyncio.get_event_loop_policy()) is WindowsProactorEventLoopPolicy:
            # WindowsProactorEventLoopPolicy is not compatible with tornado 6
            # fallback to the pre-3.8 default of Selector
            # 注释掉此行:asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
            print(f'do not change to Selector Event Loop') #加入一行提示

则下述代码运行通过:

from playwright.async_api import async_playwright

playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless = False,)

c=await browser.new_context(viewport={'width':1024,'height':768})

page = await c.new_page()

await page.goto("http://image.baidu.com")

await page.locator("#kw").fill("XXX...")

await page.locator("#homeSearchForm > span.s_btn_wr > input").click()

async with  page.expect_popup() as page1_info:
     await page.locator("#imgid > div > ul > li:nth-child(6)").click()
page1 = await page1_info.value

await browser.close()
await playwright.stop()

此问题产生的原因,在这篇文章中有所描述:(Running Playwright in JupyterLab Notebook Problem - Not implemented Error - #2 by fomightez - JupyterLab - Jupyter Community Forum

It appears that the problem is specific to Windows Platform and as identified in the original question as the use of SelectorEventLoopPolicy rather than ProactorEventloopPolicy in JupyterLabs is the source of the issue.

Since SelectorEventLoopPolicy does not support Pipes the Not implemented error occurs when Playwright first tries to open a pipe.

...

#asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

Comment out that line,

Not sure if this will have any adverse effects on other operations, but it does cause the original code to execute in JupyterLab.
似乎这个问题是特定于Windows平台的,并且在原始问题中确定为在JupyterLabs中使用SelectorEventLoopPolicy而不是ProactorEventloopPolicy是问题的根源。

由于SelectorEventLoopPolicy不支持管道,当playwright第一次尝试打开管道时,会出现not implemented错误。

#asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

注释掉这行代码,

不确定这是否会对其他操作产生任何不利影响,但它确实会导致原始代码在JupyterLab中执行。

目前microsoft在github playwright的bug回复里,这个问题并没有得到解决。所以这只能是一时之选。

相关推荐
易辰君1 小时前
Python编程 - 协程
开发语言·python
宇宙第一小趴菜1 小时前
探索网络世界:TCP/IP协议、Python Socket编程及日常生活比喻
网络·python·tcp/ip
小黑031 小时前
Phoenix使用
开发语言·python
南斯拉夫的铁托1 小时前
(PySpark)RDD实验实战——求商品销量排行
python·spark·pyspark
kay_5451 小时前
YOLOv8改进 | 模块缝合 | C2f 融合SCConv提升检测性能【CVPR2023】
人工智能·python·深度学习·yolo·目标检测·面试·yolov8改进
kngines1 小时前
【PLW004】基于Python网络爬虫与推荐算法的新闻推荐平台v1.0(Python+Django+NLP+Vue+MySQL前后端分离)
爬虫·python·nlp·推荐算法
激动的兔子2 小时前
Python_两个jpg图片文件名称互换
python
计算机学姐2 小时前
基于python+django+vue的医院预约挂号系统
开发语言·vue.js·后端·python·mysql·django·tornado
嵌入式杂谈2 小时前
计算机视觉——基于OpenCV和Python进行模板匹配
python·opencv·计算机视觉
六个核桃Lu2 小时前
图像处理与OCR识别的实践经验(2)
图像处理·人工智能·python·opencv·ocr