我尝试在 selenium 中编写代码的输入字段:
input class="tt-hint" type="text" disabled="" spellcheck="off" autocomplete="off" style="position: absolute; top: 0px; left: 0px; border-color: transparent; box-shadow: none; background: none repeat scroll 0% 0% rgb(255, 255, 255);"
我的代码是:
WebElementy inp= driver.findElement(By.className("tt-hint"));
inp.sendKeys(new String[] { "mo" });
但是上面的代码不起作用。我不断收到的错误是:
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions
感谢任何帮助。
我已将代码修改为
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].removeAttribute('disabled')",inp);
inp.sendKeys("mo"); I get the output as
最佳答案
异常说明了一切。该元素尚未准备好接受任何交互并已禁用。 JavaScript 是这里唯一的选项。我将删除 disabled
属性,然后使用 sendKeys()
String script = "document.getElementsByClassName('tt-hint')[1].removeAttribute('disabled')";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(script);
WebElementy inp= driver.findElement(By.className("tt-hint"));
inp.sendKeys("Whatever");
关于java - 无法对 selenium 中禁用的元素使用发送键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31411644/