如何为 HtmlUnit 中没有 value 属性的文本字段提供值?

How to provide a value for text field that does not have the value attribute in HtmlUnit?

下面的 HTML 元素是表单的一部分:

<input id="code" type="text" name="code" placeholder="Enter code here" autocomplete="off"/>

我想将一个字符串传递给这个元素。我不知道如何传递没有值属性的字符串。

您可以尝试使用setText()方法将字符串传递给输入:

HtmlTextInput input = (HtmlTextInput) page.getElementById("code");
input.setText("your_text");

type()方法:

HtmlTextInput input = (HtmlTextInput) page.getElementById("code");
input.type("your_text");