Python Playwright 文本框操作技巧
在本文中,將詳細介紹Playwright的文本框操作, 包括如何獲得文本框的值, 以及向文本框中添加單行和多行文本。
田辛老師將用網(wǎng)上的一個測試畫面來進行說明:
URL:https://demoqa.com/text-box

F12 查找網(wǎng)站源碼,我們可以知道這四個Textbox元素的元素id。
- userName
- userEmail
- currentAddress
- permanentAddress
1 填充單行文本
我們可以使用頁面對象的 page.locator() 方法來查找元素,并使用 fill() 方法來輸入內(nèi)容。
# 輸入Full Name
page.locator("#userName").fill("Your Name")2 填充多行文本
對于多行文本來說, 方法和單行文本一致。 只不過需要通過\n來進行分行。
# 填充地址
page.locator("#currentAddress").fill("Your current address\nYour current address 2\nYour current address 3")3 獲取文本框的值
使用input_value()方法獲得文本框的值。
print(page.locator("#userName").input_value())
print(page.locator("#currentAddress").input_value())4 完整代碼
老規(guī)矩, 完整代碼示例:
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# Go to https://demoqa.com/text-box
page.goto("https://demoqa.com/text-box")
# Fill #userName
page.locator("#userName").fill("Your Name")
# Fill #userEmail
page.locator("#userEmail").fill("your.name@yourdomain.com")
# Fill #currentAddress
page.locator("#currentAddress").fill("Your current address\nYour current address 2\nYour current address 3")
# Fill #permanentAddress
page.locator("#permanentAddress").fill("Your permanent address 1\nYour permanent address 2\nYour permanent address 3")
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)執(zhí)行結(jié)果:

到此這篇關于Python Playwright 文本框操作的文章就介紹到這了,更多相關Python Playwright 文本框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
簡要講解Python編程中線程的創(chuàng)建與鎖的使用
這篇文章主要介紹了簡要講解Python編程中線程的創(chuàng)建與鎖的使用,Python中雖然有GIL的存在,但依然是能夠創(chuàng)建多個線程來交替使用的,需要的朋友可以參考下2016-02-02
python用pyecharts實現(xiàn)地圖數(shù)據(jù)可視化
這篇文章主要介紹了python用pyecharts實現(xiàn)地圖數(shù)據(jù)可視化,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-03-03
Django makemigrations migrate執(zhí)行成功但不創(chuàng)建數(shù)據(jù)庫表的解決
這篇文章主要介紹了Django makemigrations migrate執(zhí)行成功但不創(chuàng)建數(shù)據(jù)庫表的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Python HTMLTestRunner測試報告view按鈕失效解決方案
這篇文章主要介紹了Python HTMLTestRunner測試報告view按鈕失效解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05

