如何在输入框中输入整数值

How to enter integer value into input box

我有以下HTML个标签,我想pass/sendkeys输入框

<input type="number" value="null" step="1" min="0" max="2" class="abc">

'sendkeys' 不工作,因为上面的输入 'type' 是 "number"。(使用 sendkeys 它只接受 "string" 值)。

我认为 SendKeys() 会做到这一点。到目前为止,我知道您可以将任何您想要的字符串作为字符串发送,而页面应该将其作为 int 进行操作。但是,如果不是这种情况,您可以使用 JavaScriptExecutor 设置属性值 value

    string value = "1234";
    ((IJavaScriptExecutor) Driver).ExecuteScript(
        "document.querySelector(\"[type='number'][step='1']\").setAttribute('value','" + value + "');");

注意:上面的代码是 C# 代码,在 querySelector 中你可以使用任何有效的 cssSelector