欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?playwright?庫上傳和下載操作(自動化測試?playwright)

 更新時間:2023年05月17日 09:33:19   作者:番茄牛腩不吃番茄  
這篇文章主要介紹了python?playwright?庫上傳和下載操作(自動化測試?playwright?),playwright中的上傳和下載比selenium的上傳和下載要簡便些,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

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ù)庫的方法

    這篇文章主要介紹了在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法,本文示例基于debian系的Linux系統(tǒng),需要的朋友可以參考下
    2015-05-05
  • Python Matplotlib條形圖之垂直條形圖和水平條形圖詳解

    Python Matplotlib條形圖之垂直條形圖和水平條形圖詳解

    這篇文章主要為大家詳細(xì)介紹了Python Matplotlib條形圖之垂直條形圖和水平條形圖,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python日志處理模塊logging用法解析

    Python日志處理模塊logging用法解析

    這篇文章主要介紹了Python日志處理模塊logging用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • 使用NumPy和pandas對CSV文件進行寫操作的實例

    使用NumPy和pandas對CSV文件進行寫操作的實例

    今天小編就為大家分享一篇使用NumPy和pandas對CSV文件進行寫操作的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別)

    opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別)

    本文主要介紹了opencv+python識別七段數(shù)碼顯示器的數(shù)字(數(shù)字識別),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析

    Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析

    這篇文章會基于Python對微信好友進行數(shù)據(jù)分析,這里選擇的維度主要有:性別、頭像、簽名、位置,主要采用圖表和詞云兩種形式來呈現(xiàn)結(jié)果,其中,對文本類信息會采用詞頻分析和情感分析兩種方法,感興趣的小伙伴可以了解一下
    2021-12-12
  • python解包概念及實例

    python解包概念及實例

    在本篇文章里小編給大家分享的是一篇關(guān)于python解包知識點總結(jié),對此有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • python實現(xiàn)指定字符串補全空格的方法

    python實現(xiàn)指定字符串補全空格的方法

    這篇文章主要介紹了python實現(xiàn)指定字符串補全空格的方法,涉及Python中rjust,ljust和center方法的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Python根據(jù)區(qū)號生成手機號碼的方法

    Python根據(jù)區(qū)號生成手機號碼的方法

    這篇文章主要介紹了Python根據(jù)區(qū)號生成手機號碼的方法,涉及Python隨機數(shù)與字符串的相關(guān)操作技巧,需要的朋友可以參考下
    2015-07-07
  • djano一對一、多對多、分頁實例代碼

    djano一對一、多對多、分頁實例代碼

    在本篇文章里小編給大家整理的是關(guān)于djano一對一,多對多,分頁實例代碼以及相關(guān)知識點,需要的朋友們學(xué)習(xí)下。
    2019-08-08

最新評論