首页 > 编程语言 >Playwright- python 快速开始

Playwright- python 快速开始

时间:2023-10-16 17:15:33浏览次数:39  
标签:playwright python await Playwright webkit async 快速 page browser

Playwright模块提供了一种启动浏览器实例的方法。以下是使用Playwright驱动自动化的典型示例:

from playwright.sync_api import sync_playwright

def run(playwright):
    chromium = playwright.chromium # or "firefox" or "webkit".
    browser = chromium.launch()
    page = browser.new_page()
    page.goto("http://example.com")
    # other actions...
    browser.close()

with sync_playwright() as playwright:
    run(playwright)
import asyncio
from playwright.async_api import async_playwright

async def run(playwright):
    chromium = playwright.chromium # or "firefox" or "webkit".
    browser = await chromium.launch()
    page = await browser.new_page()
    await page.goto("http://example.com")
    # other actions...
    await browser.close()

async def main():
    async with async_playwright() as playwright:
        await run(playwright)
asyncio.run(main())

方法:stop

playwright.stop()
终止这个Playwright实例,以防它是绕过Python上下文管理器创建的。这在REPL应用程序中很有用(_repl 应用_本质其实就是一个shell脚本)。

>>> from playwright.sync_api import sync_playwright

>>> playwright = sync_playwright().start()

>>> browser = playwright.chromium.launch()
>>> page = browser.new_page()
>>> page.goto("http://whatsmyuseragent.org/")
>>> page.screenshot(path="example.png")
>>> browser.close()

>>> playwright.stop()

属性:chromium、firefox、webkit

playwright.chromium
playwright.firefox
playwright.webkit
此对象可用于启动或连接chromium、firefox、webkit,返回Browser的实例。

devices

playwright.devices
返回用于browser.new_context()或browser.nnew_page()的设备字典。

from playwright.sync_api import sync_playwright

def run(playwright):
    webkit = playwright.webkit
    iphone = playwright.devices["iPhone 6"]
    browser = webkit.launch()
    context = browser.new_context(**iphone)
    page = context.new_page()
    page.goto("http://example.com")
    # other actions...
    browser.close()

with sync_playwright() as playwright:
    run(playwright)
import asyncio
from playwright.async_api import async_playwright

async def run(playwright):
    webkit = playwright.webkit
    iphone = playwright.devices["iPhone 6"]
    browser = await webkit.launch()
    context = await browser.new_context(**iphone)
    page = await context.new_page()
    await page.goto("http://example.com")
    # other actions...
    await browser.close()

async def main():
    async with async_playwright() as playwright:
        await run(playwright)
asyncio.run(main())

request

playwright.request
公开可用于Web API测试的API。

selectors

playwright.selectors
选择器可用于安装自定义选择器引擎。有关详细信息,请参阅可扩展性

 

标签:playwright,python,await,Playwright,webkit,async,快速,page,browser
From: https://www.cnblogs.com/Im-Victor/p/17767778.html

相关文章

  • python如何打包成应用
    使用pyinstall如何把python代码打包成exe可执行文件的步骤、并简要介绍打包原理,最后又介绍了一个简单用例和一个带客户端界面的用例。 简介:PyInstaller将Python应用程序及其所有依赖项捆绑到一个包中。用户无需安装Python解释器或任何模块即可运行打包的应用程序。PyInstal......
  • Python实现PDF转换文件格式
    最近工作中经常遇到收到其他人提供的pdf文档,想要编辑修改下或者复制部分内容比较困难,想通过现有的pdf工具软件转换文档格式,基本都要充钱,为了免费实现pdf转换工具,网上查了下相关技术方案,整理了下代码,测试真实有效,分享下。 第一步,安装相关第三方库pipinstallPyMuPDF-ihttps:......
  • python - bleak的低功耗蓝牙设备连接
    1.接收蓝牙日志可以用手机拨号##5959##打开蓝牙调试然后使用对应的手机app先使用对应的蓝牙产品2.蓝牙日志分析将手机上的蓝牙日志文件(btsnoop_hci.log)拷贝到电脑上使用wireshark打开,主要是看发送和接收的数据,以下是某体重秤3.寻找设备importasynciofrombleakimp......
  • 圆心科技助力公立医院提升医疗服务质量,快速打造智慧医院
    随着云计算、大数据、物联网、区块链、新一代互联网通信等新技术的不断发展,“新基建”的不断升级,新医改的不断深化,智慧医院成为我国医院现代化建设的重要发展方向。在这样的背景下,为助力智慧医疗建设长效发展,我国医疗健康企业北京圆心科技集团股份有限公司基于技术优势与数字......
  • python create_future
      importasyncioasyncdefmain():loop=asyncio.get_running_loop()future=loop.create_future()print("Futurecreated:",future)awaitasyncio.sleep(1)future.set_result("Hello,World!")print("Resul......
  • Python爬虫:抖音 JS XB逆向解析
    哈喽兄弟们,抖音现在有JS加密,以前的方法爬不了饿了,今天来实现一下某音短视频的JS逆向解析。知识点动态数据抓包`在这里插入代码片`requests发送请求X-Bogus 参数逆向环境模块python 3.8               运行代码pycharm 2022.3           辅......
  • Python处理Request请求
    一、HTTP知识:request请求方式有GET/POST/PUT/PATCH/DELETE/COPY/HEAD/OPTIONS/LINK/VIEW等常用的request请求有:get和post两种形式。1.GET用于获取资源,当采用GET方式请求指定资源时,被访问的资源经服务器解析后立即返回响应内容。通常以GET方式请求特定资源时,请求中不应该......
  • python封装https请求
    importhttp.clientimportjsonclassHTTPS_Connection:def__init__(self,res_type,body,url,api_path,headers):self.res_type=res_type#接口请求类型self.body=body#请求参数self.url=url#请求服务地址......
  • python准备工作
    准备工作导学:为什么学python?python1.简洁高效2.应用场景丰富 人生苦短,我用python Python语言基础入门什么是编程语言?用于与人类和计算机进行交流的一种语言,通过编写编程语言的代码,去指挥计算机工作。python的安装安装完验证: 我的第一个程序  Pythoncharm......
  • 树叶识别系统python+Django网页界面+TensorFlow+算法模型+数据集+图像识别分类
    一、介绍树叶识别系统。使用Python作为主要编程语言开发,通过收集常见的6中树叶('广玉兰','杜鹃','梧桐','樟叶','芭蕉','银杏')图片作为数据集,然后使用TensorFlow搭建ResNet50算法网络模型,通过对数据集进行处理后进行模型迭代训练,得到一个识别精度较高的H5模型文件。并基于Dja......