Playwright系列(8):认识playwright 相关库

简介: Playwright系列(8):认识playwright 相关库

大家好,前面把基本概念介绍完了。今天开始正式介绍如何使用python 去写playwright 脚本。


安装


Pip方式


pip install --upgrade pippip install playwrightplaywright install


Conda方式安装


conda config --add channels conda-forgeconda config --add channels microsoftconda install playwrightplaywright install


这些命令会下载 Playwright 包以及安装相关浏览器Chromium, Firefox and WebKit。


使用这些库


安装好了之后,就可以在一个Python脚本中导入Playwright,然后启动这三个浏览器中 的一个(chromium, firefox and webkit)。


from playwright.sync_api import sync_playwrightwith sync_playwright() as p:    browser = p.chromium.launch()    page = browser.new_page()    page.goto("http://playwright.dev")    print(page.title())    browser.close()


Playwright 支持2个类型的API: 同步和异步。如果你的项目使用的异步, 那么你就可以用异步的API 。


import asynciofrom playwright.async_api import async_playwrightasync def main():    async with async_playwright() as p:        browser = await p.chromium.launch()        page = await browser.new_page()        await page.goto("http://playwright.dev")        print(await page.title())        await browser.close()asyncio.run(main())


你的第一个Playwright脚本


在第一个脚本中,我们将访问whatsmyuseragent.org 然后截个图。


from playwright.sync_api import sync_playwrightwith sync_playwright() as p:    browser = p.webkit.launch()    page = browser.new_page()    page.goto("http://whatsmyuseragent.org/")    page.screenshot(path="example.png")    browser.close()


默认情况下, Playwrright 使用浏览器的无头模式。 如果想关闭这个模式,上设置 headless=False 就可以启动浏览器了。也可以用 slow_mo 让脚本执行变慢.



firefox.launch(headless=False, slow_mo=50)


交互模式 (REPL)


也可以启动python 的交互模式 REPL:


python

然后快速启动Playwright


>>> from playwright.sync_api import sync_playwright>>> playwright = sync_playwright().start()# Use playwright.chromium, playwright.firefox or playwright.webkit# Pass headless=False to launch() to see the browser UI>>> browser = playwright.chromium.launch()>>> page = browser.new_page()>>> page.goto("http://whatsmyuseragent.org/")>>> page.screenshot(path="example.png")>>> browser.close()>>> playwright.stop()


也可以用异步模式:


python -m asyncio


>>> from playwright.async_api import async_playwright>>> playwright = await async_playwright().start()>>> browser = await playwright.chromium.launch()>>> page = await browser.new_page()>>> await page.goto("http://whatsmyuseragent.org/")>>> await page.screenshot(path="example.png")>>> await browser.close()>>> await playwright.stop()


Pyinstaller


也可以用 Playwright 的 Pyinstaller来创建一个独立的执行脚本.


# main.pyfrom playwright.sync_api import sync_playwrightwith sync_playwright() as p:    browser = p.chromium.launch()    page = browser.new_page()    page.goto("http://whatsmyuseragent.org/")    page.screenshot(path="example.png")    browser.close()


如果想绑定浏览器到执行脚本上, 可以设置如下配置。


Bash


PLAYWRIGHT_BROWSERS_PATH=0 playwright install chromiumpyinstaller -F main.py

PowerShell


$env:PLAYWRIGHT_BROWSERS_PATH="0"playwright install chromiumpyinstaller -F main.py


Batch


set PLAYWRIGHT_BROWSERS_PATH=0playwright install chromiumpyinstaller -F main.py


已知问题


time.sleep()会导致状态过期

很可能你不需要手动等待,因为Playwright有自动等待功能。如果你仍然依赖它,你应该使用page.wait_for_timeout(5000)而不是time.sleep(5),最好是完全不等待超时,但有时它对调试是有用的。在这些情况下,使用我们的等待(wait_for_timeout)方法而不是时间模块。这是因为我们内部依赖于异步操作,当使用time.sleep(5)时,它们不能得到正确处理。


与Windows上Asyncio的SelectorEventLoop不兼容

Playwright在一个子进程中运行驱动,所以在Windows上需要asyncio的ProactorEventLoop,因为SelectorEventLoop不支持异步子进程。


在Windows的Python 3.7上,Playwright将默认的事件循环设置为ProactorEventLoop,因为它在Python 3.8以上是默认的。


线程


Playwright的API不是线程安全的。如果你在多线程环境中使用Playwright,你应该为每个线程创建一个playwright实例。


如果觉得阿萨的内容对你有帮助,欢迎围观点赞。

相关文章
|
7月前
|
测试技术 Python
Playwright系列(4):录制测试脚本
Playwright系列(4):录制测试脚本
214 0
|
28天前
|
Web App开发 JavaScript 测试技术
Playwright 测试夹具
Playwright 测试夹具
11 1
|
26天前
|
Web App开发 iOS开发 C++
Playwright 运行项目。
Playwright 运行项目。
34 3
|
4月前
|
数据采集 JavaScript 前端开发
Puppeteer-py:Python 中的无头浏览器自动化
Puppeteer-py:Python 中的无头浏览器自动化
|
7月前
|
前端开发 测试技术
使用 Playwright 进行元素定位
本文介绍了Playwright在自动化测试和网页爬取中如何定位页面元素。通过CSS选择器、XPath和文本内容等方式,Playwright的`locator`对象允许灵活定位。示例代码展示了使用`query_selector()`、XPath表达式以及`with_text()`方法找到元素并获取其文本。此外,文章还提到了其他内置定位方法,如根据角色、标签文本、占位符等属性进行定位,并提供了相关使用示例。总之,Playwright提供了丰富的元素定位方法,便于高效地进行网页操作。
|
7月前
|
Web App开发 测试技术 C++
Playwright安装与Python集成:探索跨浏览器测试的奇妙世界
Playwright是新兴的跨浏览器测试工具,相比Selenium,它支持Chrome、Firefox、WebKit,执行速度快,选择器更稳定。安装Playwright只需一条`pip install playwright`的命令,随后的`playwright install`会自动添加浏览器,无需处理浏览器驱动问题。这一优势免去了Selenium中匹配驱动的烦恼。文章适合寻求高效自动化测试解决方案的开发者。
|
7月前
|
Web App开发 Linux C++
Playwright系列(7):用VSCode 开始写Playwright 脚本
Playwright系列(7):用VSCode 开始写Playwright 脚本
962 0
Playwright系列(7):用VSCode 开始写Playwright 脚本
|
7月前
|
Web App开发 Linux iOS开发
PlayWright 系列1:安装
PlayWright 系列1:安装
172 0
|
7月前
|
Web App开发 存储 测试技术
Playwright系列(11):Playwright命令行
Playwright系列(11):Playwright命令行
433 0