剧作家Java:如何获得输入字段的值

发布于 2025-02-11 12:55:07 字数 428 浏览 2 评论 0原文

您好剧作家专家,

我有这个输入字段

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁许谁一生繁华 2025-02-18 12:55:07

TextContext不会返回输入的值。如果您在DevTools中尝试,也会发生同样的事情。
您可以使用 inputValue 而不是。
例如

page.locator("#myName").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.

page.locator("#myName").inputValue();
单调的奢华 2025-02-18 12:55:07

如果您要断言自己的价值,则可以使用.hasvalue断言。您可以从 playwright文档。

assertThat(page.locator("#myName")).hasValue("input-value");

In case you want to assert your value you can use the .hasValue assertion. You can read more about it from the playwright docs.

assertThat(page.locator("#myName")).hasValue("input-value");
孤独患者 2025-02-18 12:55:07

您也可以使用更多的IMHO更通用getAttribute

page.locator("#myName").getAttribute("placeholder");

documentation: https://playwright.docs/api/class-locator#locator-get-atribute

You could also use the imho more generic getAttribute:

page.locator("#myName").getAttribute("placeholder");

Documentation: https://playwright.dev/docs/api/class-locator#locator-get-attribute

梦境 2025-02-18 12:55:07

理论是

InputValue()用于检索您刚键入的输入元素的值。

TextContent()是一种用于在网页上获取元素的可见文本内容的方法。它可以是标题标签。此方法对于网络刮擦和验证页面的内容很有用。

这是一个代码示例,

page.navigate("https://the-internet.herokuapp.com/forgot_password");
Locator inputTextField=page.locator("//input[@id='email']");


// Get input text of an element using inputValue();
String actualInputVal=inputTextField.inputValue();
System.out.println("actualInputVal=="+actualInputVal);
// Assert the value of it using  hasValue
assertThat(inputTextField).hasValue("My input");


Locator forgetPasswordHeader=page.locator("//h2[text()='Forgot Password']");
String forgetPasswordHeaderActualText=forgetPasswordHeader.textContent();
System.out.println("forgetPasswordHeaderActualText=="+forgetPasswordHeaderActualText);
// you can't use hasValue here since  it's not an input element so use hasText
assertThat(forgetPasswordHeader).hasText("Forgot Password");

这将打印

actualInputVal==My input
forgetPasswordHeaderActualText==Forgot Password

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

page.navigate("https://the-internet.herokuapp.com/forgot_password");
Locator inputTextField=page.locator("//input[@id='email']");


// Get input text of an element using inputValue();
String actualInputVal=inputTextField.inputValue();
System.out.println("actualInputVal=="+actualInputVal);
// Assert the value of it using  hasValue
assertThat(inputTextField).hasValue("My input");


Locator forgetPasswordHeader=page.locator("//h2[text()='Forgot Password']");
String forgetPasswordHeaderActualText=forgetPasswordHeader.textContent();
System.out.println("forgetPasswordHeaderActualText=="+forgetPasswordHeaderActualText);
// you can't use hasValue here since  it's not an input element so use hasText
assertThat(forgetPasswordHeader).hasText("Forgot Password");

This will print

actualInputVal==My input
forgetPasswordHeaderActualText==Forgot Password
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文