python+playwright 元素操作示例代碼
Playwright 可以與 HTML 輸入元素交互,例如文本輸入、復(fù)選框、單選按鈕、選擇選項、鼠標(biāo)單擊、鍵入字符、鍵和快捷方式以及上傳文件和焦點元素。
fill() 輸入文字
使用 locator.fill() 是填寫表單字段的最簡單方法。它聚焦元素并input使用輸入的文本觸發(fā)事件。它適用于 <input> , <textarea> 和 [contenteditable] 元素。
同步示例
# Text 文本框輸入
page.get_by_role("textbox").fill("Peter")
# 根據(jù)label 定位 Date 日期輸入
page.get_by_label("Birth date").fill("2020-02-02")
# Time input
page.get_by_label("Appointment time").fill("13:15")
# Local datetime input
page.get_by_label("Local time").fill("2020-03-02T05:15")異步示例
# Text input
await page.get_by_role("textbox").fill("Peter")
# Date input
await page.get_by_label("Birth date").fill("2020-02-02")
# Time input
await page.get_by_label("Appointment time").fill("13:15")
# Local datetime input
await page.get_by_label("Local time").fill("2020-03-02T05:15")鼠標(biāo)點擊 click()
# Generic click
page.get_by_role("button").click()
# Double click
page.get_by_text("Item").dblclick()
# Right click
page.get_by_text("Item").click(button="right")
# Shift + click
page.get_by_text("Item").click(modifiers=["Shift"])
# Hover over element
page.get_by_text("Item").hover()
# Click the top left corner
page.get_by_text("Item").click(position={ "x": 0, "y": 0})文件上傳
可以使用locator.set_input_files()方法選擇要上傳的輸入文件。它期望第一個參數(shù)指向類型為 的輸入元素"file"。數(shù)組中可以傳遞多個文件。如果某些文件路徑是相對的,則它們將相對于當(dāng)前工作目錄進(jìn)行解析。空數(shù)組清除所選文件。
# Select one file
page.get_by_label("Upload file").set_input_files('myfile.pdf')
# Select multiple files
page.get_by_label("Upload files").set_input_files(['file1.txt', 'file2.txt'])
# Remove all the selected files
page.get_by_label("Upload file").set_input_files([])
# Upload buffer from memory
page.get_by_label("Upload file").set_input_files(
files=[
{"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
],
)select 下拉框
# Single selection matching the value
page.get_by_label('Choose a color').select_option('blue')
# Single selection matching the label
page.get_by_label('Choose a color').select_option(label='Blue')
# Multiple selected items
page.get_by_label('Choose multiple colors').select_option(['red', 'green', 'blue'])復(fù)選框和單選
# Check the checkbox
page.get_by_label('I agree to the terms above').check()
# Assert the checked state
assert page.get_by_label('Subscribe to newsletter').is_checked() is True
# Select the radio button
page.get_by_label('XL').check()focus()聚焦給定元素
page.get_by_label('password').focus()drag_to 拖動元素
此方法將:
- 將鼠標(biāo)懸停在要拖動的元素上。
- 按鼠標(biāo)左鍵。
- 將鼠標(biāo)移動到將接收放置的元素。
- 松開鼠標(biāo)左鍵。
page.locator("#item-to-be-dragged").drag_to(page.locator("#item-to-drop-at"))到此這篇關(guān)于python+playwright 元素操作的文章就介紹到這了,更多相關(guān)python playwright 元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Playwright模擬API的項目實踐
- Python自動化神器Playwright的用法詳解
- Python寫UI自動化之playwright(點擊操作)詳解
- Python?Playwright進(jìn)行常見的頁面交互操作
- 深入理解Playwright的高級功能和用法
- Python中playwright啟動瀏覽器與常見運行方式詳解
- Python中Playwright模塊進(jìn)行自動化測試的實現(xiàn)
- 使用Python中的Playwright制作測試視頻的實現(xiàn)步驟
- 使用Playwright進(jìn)行視覺回歸測試詳解
- python playwright--pytest-playwright、pytest-base-url插件編寫用例
- Playwright 跟蹤查看器的高級用法
相關(guān)文章
利用Python中的pandas庫對cdn日志進(jìn)行分析詳解
這篇文章主要介紹了利用Python中的pandas庫進(jìn)行cdn日志分析的相關(guān)資料,文中分享了pandas對cdn日志分析的完整示例代碼,然后詳細(xì)介紹了關(guān)于pandas庫的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03
Python實現(xiàn)爬取網(wǎng)頁中動態(tài)加載的數(shù)據(jù)
這篇文章主要介紹了Python實現(xiàn)爬取網(wǎng)頁中動態(tài)加載的數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python?matplotlib.pyplot.hist()繪制直方圖的方法實例
直方圖(Histogram)又稱質(zhì)量分布圖,是一種統(tǒng)計報告圖,由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況,一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib.pyplot.hist()繪制直方圖的相關(guān)資料,需要的朋友可以參考下2022-06-06
Python2和Python3之間的str處理方式導(dǎo)致亂碼的講解
今天小編就為大家分享一篇關(guān)于Python2和Python3之間的str處理方式導(dǎo)致亂碼的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
如何基于Python + requests實現(xiàn)發(fā)送HTTP請求
這篇文章主要介紹了如何基于Python + requests實現(xiàn)發(fā)送HTTP請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01

