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"]')
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')
expect_file_chooser
方法是官方提供的上传方法,不作过多解释,用就是了fc_info.value
是获取到了上传的相关元素,然后赋值给file_chooser
- 调用
set_files
方法,然后传入文件路径
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"]')
page.click('//*[text()="导入导出"]')
time.sleep(2)
with page.expect_download() as download_info:
page.click('//*[@href="//域名/template/导入知识目录.xlsx"]')
download = download_info.value
print(download)
print(download.path())
- 下载和上传的区别就是必须要引入上下文,官网提供的上下文就是使用
browser.new_context(accept_downloads=True)
,方法的括号中必须要有accept_downloads=True
,不然运行就会报错。 expect_download
方法是官方提供的下载方法,不作过多解释,用就是了download_info.value
是获取到了上传的相关元素,然后赋值给download
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"]')
page.click('//*[text()="导入导出"]')
time.sleep(2)
with page.expect_download() as download_info:
page.click('//*[@href="//域名/template/导入知识目录.xlsx"]')
download = download_info.value
download.save_as('E:\playwrightPyinstaller\导入知识目录.xlsx')
download.save_as(path)
方法即是官方提供的自定义路径存储,path
路径必须要是路径和文件名称在一起,不然就会抛出权限不足的错误异常。
以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;非常期待你的点赞和分享哟,谢谢!
未完,待续…
一直都在努力,希望您也是!
微信搜索公众号:就用python
文章作者:李 锋|编辑排版:梁莉莉