Playwright:修订间差异
(创建页面,内容为“Playwright是微软源的一个Web测试和自动化框架。支持 Chromium、Firefox和WebKit浏览器,Linux、macOS和Windows平台,Python、.NET和Java等多语言。 ==简介== ===时间轴=== ===安装=== 安装Python版本: <syntaxhighlight lang="bash" > # 安装pytest插件版playwright # pip install pytest-playwright # 安装Pytest pip install playwright # 安装所有支持的浏览器及配置驱动 # playwright ins…”) |
无编辑摘要 |
||
第20行: | 第20行: | ||
{{了解更多 | {{了解更多 | ||
|[https://playwright.dev/python/docs/intro Playwright Python 文档:安装] | |[https://playwright.dev/python/docs/intro Playwright Python 文档:安装] | ||
|[https://playwright.dev/python/docs/library Playwright Python 文档:入门] | |||
}} | }} | ||
==快速入门== | ==快速入门== | ||
=== 同步模式 === | |||
<syntaxhighlight lang="python" > | |||
from playwright.sync_api import sync_playwright | |||
playwright = sync_playwright().start() | |||
# 使用playwright.chromium, playwright.firefox or playwright.webkit | |||
# 默认无界面模式,launch使用headless=False设置有界面 | |||
browser = playwright.firefox.launch(headless=False) | |||
page = browser.new_page() | |||
page.goto("https://www.baidu.com") | |||
page.screenshot(path="截图.png") | |||
browser.close() | |||
playwright.stop() | |||
</syntaxhighlight> | |||
更常用使用with语句: | |||
<syntaxhighlight lang="python" > | |||
from playwright.sync_api import sync_playwright | |||
with sync_playwright() as p: | |||
browser = p.firefox.launch() | |||
page = browser.new_page() | |||
page.goto("https://www.baidu.com/") | |||
page.screenshot(path="example.png") | |||
browser.close() | |||
</syntaxhighlight> | |||
代码在[[Jupyter]]中运行会出现错误:<code>Error: It looks like you are using Playwright Sync API inside the asyncio loop.Please use the Async API instead.</code>。解决办法:代码保存到<code>测试.py</code>,在终端运行<code>python 测试.py</code>。 | |||
{{了解更多 | |||
|[https://playwright.dev/python/docs/library Playwright Python 文档:入门] | |||
}} | |||
=== 异步模式 === | |||
使用with语句 | |||
<syntaxhighlight lang="python" > | |||
import asyncio | |||
from playwright.async_api import async_playwright | |||
async def main(): | |||
async with async_playwright() as p: | |||
browser = await p.firefox.launch(headless=False) | |||
page = await browser.new_page() | |||
await page.goto("https://wwww.baidu.com") | |||
print(await page.title()) | |||
await browser.close() | |||
asyncio.run(main()) | |||
</syntaxhighlight> | |||
{{了解更多 | |||
|[https://playwright.dev/python/docs/library Playwright Python 文档:入门] | |||
}} | |||
==资源== | ==资源== | ||
第27行: | 第78行: | ||
* Playwright 官网:https://playwright.dev | * Playwright 官网:https://playwright.dev | ||
* Playwright 源代码:https://github.com/microsoft/playwright | * Playwright 源代码:https://github.com/microsoft/playwright | ||
* Playwright Python版源代码:https://github.com/microsoft/playwright-python | |||
* Playwright Python 文档:https://playwright.dev/python/docs/intro | * Playwright Python 文档:https://playwright.dev/python/docs/intro | ||
2023年4月10日 (一) 05:49的版本
Playwright是微软源的一个Web测试和自动化框架。支持 Chromium、Firefox和WebKit浏览器,Linux、macOS和Windows平台,Python、.NET和Java等多语言。
简介
时间轴
安装
安装Python版本:
# 安装pytest插件版playwright
# pip install pytest-playwright
# 安装Pytest
pip install playwright
# 安装所有支持的浏览器及配置驱动
# playwright install
# 只安装chrome浏览器及配置驱动,使用playwright install -h可以查看帮助
# 目前支持chromium, chrome, chrome-beta, msedge, msedge-beta, msedge-dev, firefox, webkit浏览器。
playwright install chrome
快速入门
同步模式
from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
# 使用playwright.chromium, playwright.firefox or playwright.webkit
# 默认无界面模式,launch使用headless=False设置有界面
browser = playwright.firefox.launch(headless=False)
page = browser.new_page()
page.goto("https://www.baidu.com")
page.screenshot(path="截图.png")
browser.close()
playwright.stop()
更常用使用with语句:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.firefox.launch()
page = browser.new_page()
page.goto("https://www.baidu.com/")
page.screenshot(path="example.png")
browser.close()
代码在Jupyter中运行会出现错误:Error: It looks like you are using Playwright Sync API inside the asyncio loop.Please use the Async API instead.
。解决办法:代码保存到测试.py
,在终端运行python 测试.py
。
了解更多 >> Playwright Python 文档:入门
异步模式
使用with语句
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.firefox.launch(headless=False)
page = await browser.new_page()
await page.goto("https://wwww.baidu.com")
print(await page.title())
await browser.close()
asyncio.run(main())
了解更多 >> Playwright Python 文档:入门
资源
官网
- Playwright 官网:https://playwright.dev
- Playwright 源代码:https://github.com/microsoft/playwright
- Playwright Python版源代码:https://github.com/microsoft/playwright-python
- Playwright Python 文档:https://playwright.dev/python/docs/intro