python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python playwright上传和下载

python playwright 库上传和下载操作(自动化测试 playwright)

作者:番茄牛腩不吃番茄

这篇文章主要介绍了python playwright 库上传和下载操作(自动化测试 playwright ),playwright中的上传和下载比selenium的上传和下载要简便些,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

python:自动化测试 playwright 库上传和下载

今天主要是聊聊playwright中的上传和下载操作,playwright中的上传和下载比selenium的上传和下载要简便些,例:selenium中的上传还要有对话框选择文件,再点击上传,而playwright中是找到元素执行点击后设置一个文件位置。

上传

操作语法

# 选择一个文件
page.set_input_files('input#upload', 'myfile.pdf')
# 选择多个文件
page.set_input_files('input#upload', ['file1.txt', 'file2.txt'])
# 删除所有选定的文件
page.set_input_files('input#upload', [])
# 从内存上传缓冲区
page.set_input_files(
    "input#upload",
    files=[
        {"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
    ],
)

上述的代码中这种是那种含输入元素(它动态创建的)的上传操作,如果是可点击的上传操作,可以直接使用下述语法:

with page.expect_file_chooser() as fc_info:
    page.click("upload")
file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")

上传示例
该示例主要演示的是可点击的上传操作,动态输入元素的上传没找到对应示例:

import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False
    )
    page = browser.new_page()
    page.goto("https://域名/user/login")
    page.click('//*[text()="密码登录"]')
    page.click('#user_name')
    page.fill('#user_name', "账号")
    page.click('#password')
    page.fill('#password', "密码")
    page.click('//*[text()="登 录"]')
    time.sleep(2)
    page.click('//*[text()="学习资源"]')
    page.click('//*[@href="/manager/resource/directory" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ]')
    page.click('//*[text()="导入导出"]')
    time.sleep(2)
    with page.expect_file_chooser() as fc_info:
        page.click('//*[text()=" 上传文件"]')
    file_chooser = fc_info.value
    print(file_chooser)
    file_chooser.set_files('E:\playwrightPyinstaller\导入知识目录.xlsx')

print(file_chooser)打印的结果如下:

<FileChooser page=<Page url='https://域名/manager/resource/directory'> element=JSHandle@<input type="file" accept=".xlsx,.xls"/>>
Process finished with exit code 0

下载

操作语法

# 开始等待下载
with page.expect_download() as download_info:
    # 执行启动下载的操作
    page.click("button#delayed-download")
download = download_info.value
# 等待下载过程完成
path = download.path()

上述代码是下载的语法,这也是处理文件下载的最简单方法。

下载示例

import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False
    )
    context = browser.new_context(
        accept_downloads=True
    )
    page = context.new_page()
    page.goto("https://域名/user/login")
    page.click('//*[text()="密码登录"]')
    page.click('#user_name')
    page.fill('#user_name', "账号")
    page.click('#password')
    page.fill('#password', "密码")
    page.click('//*[text()="登 录"]')
    time.sleep(2)
    page.click('//*[text()="学习资源"]')
    page.click('//*[@href="/manager/resource/directory" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ]')
    page.click('//*[text()="导入导出"]')
    time.sleep(2)
    with page.expect_download() as download_info:
        page.click('//*[@href="//域名/template/导入知识目录.xlsx" rel="external nofollow"  rel="external nofollow" ]')
    download = download_info.value
    print(download)
    print(download.path())

print(file_chooser)打印的结果如下:

<Download url='https://域名/template/%E5%AF%BC%E5%85%A5%E7%9F%A5%E8%AF%86%E7%9B%AE%E5%BD%95.xlsx' suggested_filename='导入知识目录.xlsx'>
Process finished with exit code 0

print(download.path())是获取下载后的存储路径,打印的结果如下:

C:\Users\lifeng\AppData\Local\Temp\playwright-artifacts-H0nUyZ\2b71684d-7471-4055-9581-6cb364b50efc
Process finished with exit code 0

当然您也可以进行自定义存储路径,这个官方也是提供了相应方法:

import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False
    )
    context = browser.new_context(
        accept_downloads=True
    )
    page = context.new_page()
    page.goto("https://lms3.9first.com/user/login")
    page.click('//*[text()="密码登录"]')
    page.click('#user_name')
    page.fill('#user_name', "账号")
    page.click('#password')
    page.fill('#password', "密码")
    page.click('//*[text()="登 录"]')
    time.sleep(2)
    page.click('//*[text()="学习资源"]')
    page.click('//*[@href="/manager/resource/directory" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ]')
    page.click('//*[text()="导入导出"]')
    time.sleep(2)
    with page.expect_download() as download_info:
        page.click('//*[@href="//域名/template/导入知识目录.xlsx" rel="external nofollow"  rel="external nofollow" ]')
    download = download_info.value
    download.save_as('E:\playwrightPyinstaller\导入知识目录.xlsx')

download.save_as(path)方法即是官方提供的自定义路径存储,path路径必须要是路径和文件名称在一起,不然就会抛出权限不足的错误异常。

以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;非常期待你的点赞和分享哟,谢谢!

到此这篇关于python playwright 库上传和下载操作(自动化测试 playwright )的文章就介绍到这了,更多相关python playwright 库上传和下载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文