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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python調(diào)用Rust的5種方法的實現(xiàn)小結(jié)
本文主要介紹了Python調(diào)用Rust的5種方法的實現(xiàn)小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-11-11
python matplotlib中的subplot函數(shù)使用詳解
今天小編就為大家分享一篇python matplotlib中的subplot函數(shù)使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python學(xué)習(xí)筆記之Break和Continue用法分析
這篇文章主要介紹了Python學(xué)習(xí)筆記之Break和Continue用法,結(jié)合實例形式分析了Python中Break和Continue的功能、使用方法、區(qū)別及相關(guān)操作注意事項,需要的朋友可以參考下2019-08-08
Django用內(nèi)置方法實現(xiàn)簡單搜索功能的方法
這篇文章主要介紹了Django用內(nèi)置方法實現(xiàn)簡單搜索功能的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
python爬取網(wǎng)站數(shù)據(jù)保存使用的方法
這篇文章主要介紹了使用Python從網(wǎng)上爬取特定屬性數(shù)據(jù)保存的方法,其中解決了編碼問題和如何使用正則匹配數(shù)據(jù)的方法,詳情看下文2013-11-11

