我想用点击功能填充输入值,像这样
<script>
function inputfillclick() {
document.getElementById('inputidx').value="KuchBhi"
}
</script>
<input type="text" id="inputidx" name="inputidx" />
<button class="button" onclick="inputfillclick()">Fill Now</button>
但问题是我想在输入字段没有 id 的其他页面上执行此操作,我想通过 xpath 执行此操作,有什么办法吗?
<input maxlength="140" step="" enterkeyhint="done" autocomplete="off" type="password" class="uni-input-input">
xpath 是
//input[@type='password']
完整的 xpath
/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-view/uni-view[3]/uni-view[2]/uni-view[2]/uni-input/div/input
您可以使用 document.evaluate() 方法使用其 XPath 表达式选择输入元素,然后使用 value 属性设置其值。
function inputfillclick() {
var xpath = "//input[@type='password']";
var inputElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (inputElement) {
inputElement.value = "KuchBhi";
} else {
console.log("Input element not found.");
}
}
在此代码中,
document.evaluate()
方法使用给定的 XPath 表达式选择第一个具有 type
属性设置为 'password
' 的输入元素。如果找到这样的元素,其值属性将设置为 'KuchBhi
'。如果没有找到输入元素,代码会向控制台记录一条消息。
您可以在要填写输入字段的页面上使用按钮或任何其他事件处理程序调用
inputfillclick()
函数。