尝鲜~ 微软又出现好玩的了,Python 录制自动化操作,自动生成代码
Posted smallsha
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝鲜~ 微软又出现好玩的了,Python 录制自动化操作,自动生成代码相关的知识,希望对你有一定的参考价值。
自动化
==Splinter 是对 selenium的抽象,更高级用法==
Splinter
有关Splinter && selenium 博客
Playwright自动化模拟浏览器点击
palywright相关文章
playwright文档
# 安装playwright库
pip install playwright
# 安装浏览器驱动文件(安装过程稍微有点慢)
python -m playwright install
# 命令行键入 --help 可看到所有选项
python -m playwright codegen
-o:将录制的脚本保存到一个文件
--target:规定生成脚本的语言,有JS和Python两种,默认为Python
-b:指定浏览器驱动
playwright demo
python -m playwright codegen --target python -o \'my.py\' -b chromium https://www.baidu.com
同步依次打开三个浏览器,前往baidu搜索,截图后退出。
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto(\'https://baidu.com/\')
page.screenshot(path=f\'example-{browser_type.name}.png\')
browser.close()
异步异步操作可结合asyncio同时进行三个浏览器操作。
import asyncio
from playwright import async_playwright
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto(\'http://baidu.com/\')
await page.screenshot(path=f\'example-{browser_type.name}.png\')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
移动端更厉害的是,playwright还可支持移动端的浏览器模拟。 下面是官方文档提供的一段代码,模拟在给定地理位置上手机iphone 11 pro上的Safari浏览器,首先导航到maps.google.com,然后执行定位并截图。
from playwright import sync_playwright
with sync_playwright() as p:
iphone_11 = p.devices[\'iPhone 11 Pro\']
browser = p.webkit.launch(headless=False)
context = browser.newContext(
**iphone_11,
locale=\'en-US\',
geolocation={ \'longitude\': 12.492507, \'latitude\': 41.889938 },
permissions=[\'geolocation\']
)
page = context.newPage()
page.goto(\'https://maps.google.com\')
page.click(\'text="Your location"\')
page.screenshot(path=\'colosseum-iphone.png\')
browser.close()
以上是关于尝鲜~ 微软又出现好玩的了,Python 录制自动化操作,自动生成代码的主要内容,如果未能解决你的问题,请参考以下文章
微软开源最强Python自动化神器Playwright!不用写一行代码!