剧作家Java:如何获得输入字段的值
您好剧作家专家,
我有这个输入字段
<input id="myName" placeholder="e.g. Max" maxlength="15" class="xyz"/>
,我尝试了此Java代码:
page.locator("[placeholder=\"e.g. Max\"]").textContent();
它不起作用:-(但是我可以填补
page.locator("[placeholder=\"e.g. Max\"]").fill("Loren"); // this works
您可以帮忙并有一个主意吗?
谢谢您。
cheers 洛伦
Hello Playwright experts,
I have this input field
<input id="myName" placeholder="e.g. Max" maxlength="15" class="xyz"/>
I have tried this JAVA code:
page.locator("[placeholder=\"e.g. Max\"]").textContent();
It does not work :-( but I can fill
page.locator("[placeholder=\"e.g. Max\"]").fill("Loren"); // this works
Could you help and has an idea?
Thank you in advance.
Cheers
Loren
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TextContext
不会返回输入的值。如果您在DevTools中尝试,也会发生同样的事情。您可以使用 inputValue 而不是。
例如
textContext
won't return a value of an input. The same will happen if you try that in the DevTools.You can use inputValue instead.
e.g.
如果您要断言自己的价值,则可以使用
.hasvalue
断言。您可以从 playwright文档。In case you want to assert your value you can use the
.hasValue
assertion. You can read more about it from the playwright docs.您也可以使用更多的IMHO更通用
getAttribute
:documentation: https://playwright.docs/api/class-locator#locator-get-atribute
You could also use the imho more generic
getAttribute
:Documentation: https://playwright.dev/docs/api/class-locator#locator-get-attribute
理论是
InputValue()用于检索您刚键入的输入元素的值。
TextContent()是一种用于在网页上获取元素的可见文本内容的方法。它可以是标题标签。此方法对于网络刮擦和验证页面的内容很有用。
这是一个代码示例,
这将打印
Theory is that
inputValue() is used to retrieve the value of input elements that you just typed.
textContent() is a method that is used to get the visible text content of an element on a web page. it can be a header tag. This method is useful for web scraping and verifying the content of a page.
Here is a code example
This will print