python?playwright?庫上傳和下載操作(自動化測試?playwright)
python:自動化測試 playwright 庫上傳和下載
今天主要是聊聊playwright中的上傳和下載操作,playwright中的上傳和下載比selenium的上傳和下載要簡便些,例:selenium中的上傳還要有對話框選擇文件,再點擊上傳,而playwright中是找到元素執(zhí)行點擊后設置一個文件位置。
上傳
操作語法
# 選擇一個文件
page.set_input_files('input#upload', 'myfile.pdf')
# 選擇多個文件
page.set_input_files('input#upload', ['file1.txt', 'file2.txt'])
# 刪除所有選定的文件
page.set_input_files('input#upload', [])
# 從內(nèi)存上傳緩沖區(qū)
page.set_input_files(
"input#upload",
files=[
{"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
],
)上述的代碼中這種是那種含輸入元素(它動態(tài)創(chuàng)建的)的上傳操作,如果是可點擊的上傳操作,可以直接使用下述語法:
with page.expect_file_chooser() as fc_info:
page.click("upload")
file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")上傳示例
該示例主要演示的是可點擊的上傳操作,動態(tài)輸入元素的上傳沒找到對應示例:
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')expect_file_chooser方法是官方提供的上傳方法,不作過多解釋,用就是了fc_info.value是獲取到了上傳的相關(guān)元素,然后賦值給file_chooser- 調(diào)用
set_files方法,然后傳入文件路徑
print(file_chooser)打印的結(jié)果如下:
<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:
# 執(zhí)行啟動下載的操作
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="http://域名/template/導入知識目錄.xlsx" rel="external nofollow" rel="external nofollow" ]')
download = download_info.value
print(download)
print(download.path())- 下載和上傳的區(qū)別就是必須要引入上下文,官網(wǎng)提供的上下文就是使用
browser.new_context(accept_downloads=True),方法的括號中必須要有accept_downloads=True,不然運行就會報錯。 expect_download方法是官方提供的下載方法,不作過多解釋,用就是了download_info.value是獲取到了上傳的相關(guān)元素,然后賦值給download
print(file_chooser)打印的結(jié)果如下:
<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())是獲取下載后的存儲路徑,打印的結(jié)果如下:
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="http://域名/template/導入知識目錄.xlsx" rel="external nofollow" rel="external nofollow" ]')
download = download_info.value
download.save_as('E:\playwrightPyinstaller\導入知識目錄.xlsx')download.save_as(path)方法即是官方提供的自定義路徑存儲,path路徑必須要是路徑和文件名稱在一起,不然就會拋出權(quán)限不足的錯誤異常。
以上總結(jié)或許能幫助到你,或許幫助不到你,但還是希望能幫助到你,如有疑問、歧義,直接私信留言會及時修正發(fā)布;非常期待你的點贊和分享喲,謝謝!
到此這篇關(guān)于python playwright 庫上傳和下載操作(自動化測試 playwright )的文章就介紹到這了,更多相關(guān)python playwright 庫上傳和下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何通過手肘法實現(xiàn)k_means聚類詳解
K-means聚類算法是一種常見的無監(jiān)督學習算法,用于將數(shù)據(jù)集分成k個不同的簇,下面這篇文章主要給大家介紹了關(guān)于Python如何通過手肘法實現(xiàn)k_means聚類的相關(guān)資料,需要的朋友可以參考下2023-04-04
flask后端request獲取參數(shù)的幾種方式整理
這篇文章主要為大家介紹了flask后端request獲取參數(shù)的幾種方式整理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Pandas數(shù)據(jù)集的合并與連接merge()方法
Pandas數(shù)據(jù)集的合并與連接(merge())是數(shù)據(jù)處理過程中常用的操作之一,在使用Pandas進行數(shù)據(jù)集合并時,可以使用merge()函數(shù)將兩個或多個數(shù)據(jù)集按照指定的列進行合并,本文就來介紹一下,感興趣的可以了解一下2023-11-11
零基礎(chǔ)寫python爬蟲之使用urllib2組件抓取網(wǎng)頁內(nèi)容
文章詳細介紹了在python2.5環(huán)境下,如何使用urllib2這個python自帶的組件進行抓取指定網(wǎng)頁內(nèi)容的,整個過程記錄的非常的詳細,也很簡單,有需要的朋友可以參考下,寫出自己的python爬蟲2014-11-11

