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

F12 查找網(wǎng)站源碼,我們可以知道這四個(gè)Textbox元素的元素id。
- userName
- userEmail
- currentAddress
- permanentAddress
1 填充單行文本
我們可以使用頁(yè)面對(duì)象的 page.locator() 方法來(lái)查找元素,并使用 fill() 方法來(lái)輸入內(nèi)容。
# 輸入Full Name
page.locator("#userName").fill("Your Name")2 填充多行文本
對(duì)于多行文本來(lái)說(shuō), 方法和單行文本一致。 只不過(guò)需要通過(guò)\n來(lái)進(jì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é)果:

到此這篇關(guān)于Python Playwright 文本框操作的文章就介紹到這了,更多相關(guān)Python Playwright 文本框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)要講解Python編程中線程的創(chuàng)建與鎖的使用
這篇文章主要介紹了簡(jiǎn)要講解Python編程中線程的創(chuàng)建與鎖的使用,Python中雖然有GIL的存在,但依然是能夠創(chuàng)建多個(gè)線程來(lái)交替使用的,需要的朋友可以參考下2016-02-02
python用pyecharts實(shí)現(xiàn)地圖數(shù)據(jù)可視化
這篇文章主要介紹了python用pyecharts實(shí)現(xiàn)地圖數(shù)據(jù)可視化,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
python matplotlib庫(kù)繪制散點(diǎn)圖例題解析
這篇文章主要介紹了python matplotlib庫(kù)繪制散點(diǎn)圖例題解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Django makemigrations migrate執(zhí)行成功但不創(chuàng)建數(shù)據(jù)庫(kù)表的解決
這篇文章主要介紹了Django makemigrations migrate執(zhí)行成功但不創(chuàng)建數(shù)據(jù)庫(kù)表的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python HTMLTestRunner測(cè)試報(bào)告view按鈕失效解決方案
這篇文章主要介紹了Python HTMLTestRunner測(cè)試報(bào)告view按鈕失效解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
pandas把所有大于0的數(shù)設(shè)置為1的方法
今天小編就為大家分享一篇pandas把所有大于0的數(shù)設(shè)置為1的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python word轉(zhuǎn)pdf代碼實(shí)例
這篇文章主要介紹了python word轉(zhuǎn)pdf代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

