剧作家专家你好,
我有这个输入字段
<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
你能帮忙吗?
提前谢谢你。
欢呼声罗兰
textContext
不会返回输入的值。如果你在DevTools中尝试同样的情况也会发生。
你可以用inputValue代替。
。
page.locator("#myName").inputValue();
如果您想断言您的值,您可以使用.hasValue
断言。你可以从剧作家文档中了解更多。
assertThat(page.locator("#myName")).hasValue("input-value");
您也可以使用更通用的getAttribute
:
page.locator("#myName").getAttribute("placeholder");
文档:https://playwright.dev/docs/api/class-locator locator-get-attribute
理论是
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