python+playwright 元素操作示例代碼
Playwright 可以與 HTML 輸入元素交互,例如文本輸入、復選框、單選按鈕、選擇選項、鼠標單擊、鍵入字符、鍵和快捷方式以及上傳文件和焦點元素。
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")鼠標點擊 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ù)組中可以傳遞多個文件。如果某些文件路徑是相對的,則它們將相對于當前工作目錄進行解析??諗?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'])復選框和單選
# 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 拖動元素
此方法將:
- 將鼠標懸停在要拖動的元素上。
- 按鼠標左鍵。
- 將鼠標移動到將接收放置的元素。
- 松開鼠標左鍵。
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)文章
jupyter notebook tensorflow打印device信息實例
這篇文章主要介紹了jupyter notebook tensorflow打印device信息實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python在字典中獲取帶權(quán)重的隨機值實現(xiàn)方式
這篇文章主要介紹了Python在字典中獲取帶權(quán)重的隨機值,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11
Python實現(xiàn)關(guān)鍵路徑和七格圖計算詳解
這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)關(guān)鍵路徑和七格圖計算,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03
Python識別設(shè)備和操作系統(tǒng)神器device_detector使用探究
這篇文章主要介紹了Python識別設(shè)備和操作系統(tǒng)神器device_detector庫使用探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

