我正在尝试使用 Selenium Java 将值绑定到输入元素,但我不断收到错误消息
ElementNotInteractableException:键盘无法访问元素
我已经尝试了 Stack Overflow 帖子中建议的解决方案:org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard: while sending text to FirstName field in Facebook,但我仍然遇到同样的错误。
任何人都可以提出任何其他解决方案或此错误的潜在原因吗?
WebElement emailAddyField = driver.findElement(By.id("email"));
emailAddyField.sendKeys(username);
--
<div class="field-two-input-wrapper flex flex-nowrap flex-align-items-stretch flex-item-fluid relative">
<div class="flex flex-item-fluid">
<input autocomplete="off" autocapitalize="none" autocorrect="off" spellcheck="false" aria-invalid="false"
id="email" aria-describedby="id-2" class="field-two-input w100 email-input-field" value="">
</div>
<div class="field-two-input-adornment mr0-5 flex flex-align-items-center flex-item-noshrink flex-nowrap flex-gap-0-5">
<button type="button" aria-expanded="false" aria-live="assertive" aria-atomic="true" aria-invalid="false"
class="outline-none w100 flex flex-justify-space-between flex-align-items-center flex-nowrap no-pointer-events-children"
aria-label="proton.me" id="select-domain">
<span class="flex-item-fluid text-ellipsis text-left">@proton.me</span>
<svg viewBox="0 0 16 16" class="icon-16p flex-item-noshrink ml0-5" role="img" focusable="false"
aria-hidden="true">
<use xlink:href="#ic-chevron-down-filled"></use>
</svg>
</button>
</div>
</div>
谢谢。
通常情况下,如果您可以看到控件实际上是可交互的,但您的定位器无法在
DOM
中找到正确的元素,那么您应该点击您之前尝试过的环绕元素。
由于您的
input
周围只有一个包装元素同时与其他input
无关,因此我建议采用以下方法:
WebElement emailAddyField = driver.findElement(By.xpath("//input[@id='email']/parent::div"));
emailAddyField.sendKeys(username);
终于找到解决办法了。 一开始我没注意到,但
<div>
在iframe中。
下面的代码解决了这个问题。
driver.switchTo().frame("frameName");
WebElement emailAddyField = driver.findElement(By.id("email"));
emailAddyField.sendKeys(username);
driver.switchTo().defaultContent();