Python Playwright 文本框操作技巧
作者:田辛 时间:2023-01-15 20:59:21
在本文中,将详细介绍Playwright的文本框操作, 包括如何获得文本框的值, 以及向文本框中添加单行和多行文本。
田辛老师将用网上的一个测试画面来进行说明:
URL:https://demoqa.com/text-box
F12 查找网站源码,我们可以知道这四个Textbox元素的元素id。
userName
userEmail
currentAddress
permanentAddress
1 填充单行文本
我们可以使用页面对象的 page.locator()
方法来查找元素,并使用 fill()
方法来输入内容。
# 输入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 完整代码
老规矩, 完整代码示例:
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)
执行结果:
来源:https://tdouya.blog.csdn.net/article/details/130591382
标签:Python,Playwright
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
VS2008 和.NET 3.5 Beta2常见问题的解决方案
2007-09-23 12:33:00
WebStorm安装配置教程
2022-01-17 01:25:24
![](https://img.aspxhome.com/file/2023/4/131884_0s.png)
PyTorch加载预训练模型实例(pretrained)
2021-02-04 15:26:11
position、z-index、top、right、bottom和left属性
2009-10-04 20:33:00
通俗解释JavaScript正则表达式快速记忆
2024-04-22 22:24:04
![](https://img.aspxhome.com/file/2023/3/135763_0s.png)
需要使用php模板的朋友必看的很多个顶级PHP模板引擎比较分析
2023-11-19 02:10:43
用色彩打造专业的视觉效果
2010-09-25 19:04:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/01-36s.jpg)
Perl字符串处理函数大全
2023-11-25 17:33:35
JavaScript中通用的jquery动画滚屏实例
2024-04-22 22:22:34
python fire库的使用实例教程
2023-08-24 14:01:22
![](https://img.aspxhome.com/file/2023/0/61760_0s.png)
vue递归获取父元素的元素实例
2024-05-05 09:24:34
ASP.NET MVC4入门教程(八):给数据模型添加校验器
2024-06-05 09:27:38
![](https://img.aspxhome.com/file/2023/6/122816_0s.png)
dl.dt.dd.ul.li.ol区别及应用
2008-05-24 09:42:00
![](https://img.aspxhome.com/file/UploadPic/20085/24/200852494731250s.gif)
分析python请求数据
2023-09-26 08:46:08
解决PyTorch与CUDA版本不匹配的问题
2023-12-20 14:41:15
![](https://img.aspxhome.com/file/2023/6/92346_0s.jpg)
vue生成随机验证码的示例代码
2023-07-02 16:57:09
![](https://img.aspxhome.com/file/2023/3/139843_0s.png)
python高级特性和高阶函数及使用详解
2022-09-17 20:13:50
![](https://img.aspxhome.com/file/2023/4/121104_0s.png)
Sublime Text3最新激活注册码分享适用2020最新版 亲测可用
2023-02-03 07:18:38
![](https://img.aspxhome.com/file/2023/1/127861_0s.png)
如何在pyqt中实现全局事件实战记录
2023-07-01 02:25:46
![](https://img.aspxhome.com/file/2023/5/61705_0s.jpg)
ThinkPHP发送邮件示例代码
2023-11-21 19:17:31
![](https://img.aspxhome.com/file/2023/6/104176_0s.jpg)