python?playwright?庫上傳和下載操作(自動化測試?playwright)
python:自動化測試 playwright 庫上傳和下載
今天主要是聊聊playwright
中的上傳和下載操作,playwright
中的上傳和下載比selenium
的上傳和下載要簡便些,例:selenium
中的上傳還要有對話框選擇文件,再點擊上傳,而playwright
中是找到元素執(zhí)行點擊后設(shè)置一個文件位置。
上傳
操作語法
# 選擇一個文件 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)輸入元素的上傳沒找到對應(yīng)示例:
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()="學(xué)習(xí)資源"]') page.click('//*[@href="/manager/resource/directory" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]') page.click('//*[text()="導(dǎo)入導(dǎo)出"]') 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\導(dǎo)入知識目錄.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()="學(xué)習(xí)資源"]') page.click('//*[@href="/manager/resource/directory" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]') page.click('//*[text()="導(dǎo)入導(dǎo)出"]') time.sleep(2) with page.expect_download() as download_info: page.click('//*[@href="http://域名/template/導(dǎo)入知識目錄.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='導(dǎo)入知識目錄.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
當(dāng)然您也可以進行自定義存儲路徑,這個官方也是提供了相應(yīng)方法:
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()="學(xué)習(xí)資源"]') page.click('//*[@href="/manager/resource/directory" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]') page.click('//*[text()="導(dǎo)入導(dǎo)出"]') time.sleep(2) with page.expect_download() as download_info: page.click('//*[@href="http://域名/template/導(dǎo)入知識目錄.xlsx" rel="external nofollow" rel="external nofollow" ]') download = download_info.value download.save_as('E:\playwrightPyinstaller\導(dǎo)入知識目錄.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)文章
在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法
這篇文章主要介紹了在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法,本文示例基于debian系的Linux系統(tǒng),需要的朋友可以參考下2015-05-05Python Matplotlib條形圖之垂直條形圖和水平條形圖詳解
這篇文章主要為大家詳細(xì)介紹了Python Matplotlib條形圖之垂直條形圖和水平條形圖,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別)
本文主要介紹了opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析
這篇文章會基于Python對微信好友進行數(shù)據(jù)分析,這里選擇的維度主要有:性別、頭像、簽名、位置,主要采用圖表和詞云兩種形式來呈現(xiàn)結(jié)果,其中,對文本類信息會采用詞頻分析和情感分析兩種方法,感興趣的小伙伴可以了解一下2021-12-12