Python Playwright 文本框操作技巧
在本文中,將詳細(xì)介紹Playwright的文本框操作, 包括如何獲得文本框的值, 以及向文本框中添加單行和多行文本。
田辛老師將用網(wǎng)上的一個(gè)測試畫面來進(jìn)行說明:
URL:https://demoqa.com/text-box
F12 查找網(wǎng)站源碼,我們可以知道這四個(gè)Textbox元素的元素id。
- userName
- userEmail
- currentAddress
- permanentAddress
1 填充單行文本
我們可以使用頁面對象的 page.locator()
方法來查找元素,并使用 fill()
方法來輸入內(nèi)容。
# 輸入Full Name page.locator("#userName").fill("Your Name")
2 填充多行文本
對于多行文本來說, 方法和單行文本一致。 只不過需要通過\n
來進(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python虛擬環(huán)境的創(chuàng)建和使用詳解
這篇文章主要給大家介紹了關(guān)于Python虛擬環(huán)境的創(chuàng)建和使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09解決pycharm remote deployment 配置的問題
今天小編就為大家分享一篇解決pycharm remote deployment 配置的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN函數(shù)tf.nn.dynamic_rnn使用
這篇文章主要為大家介紹了python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN的tf.nn.dynamic_rnn使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05