org.openqa.selenium.ElementNotInteractableException:元素无法通过键盘访问:在向Facebook中的FirstName字段发送文本时
错误是:
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <div id="u_0_b" class="_5dbb"> is not reachable by keyboard
代码为:
System.setProperty("webdriver.gecko.driver","//Users//rozali//Documents//Selenium//geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.facebook.com");
driver.manage().window().maximize();
//entering first name
driver.findElement(By.id("u_0_b")).click();
driver.findElement(By.id("u_0_b")).sendKeys("testing it ");
//DOB
Select sel1 = new Select(driver.findElement(By.xpath(".//*[@id='month']")));
sel1.selectByIndex(4);
Select sel2 = new Select(driver.findElement(By.xpath(".//*[@id='day']")));
sel2.selectByValue("6");
Select sel3 = new Select(driver.findElement(By.xpath(".//*[@id='year']")));
sel3.selectByValue("2013");
//clicking sign up
driver.findElement(By.id("u_0_t")).click();
ElementNotInteractableException:元素无法通过键盘访问
Element is not reachable by keyboard
用简单的词表示无法使用键盘访问该元素,这意味着您甚至都不会与它进行物理交互.
ElementNotInteractableException: Element is not reachable by keyboard
Element is not reachable by keyboard
in plain words means that the element can’t be reached using the keyboard, which means you won't be physically interact with it even.
错误键盘无法访问元素可能有多种原因,该错误可能是以下之一:
There can be multiple reasons behind the error Element is not reachable by keyboard which can be either of the following:
- 该元素被隐藏,因为现代以JavaScript为中心的UI样式始终隐藏丑陋的原始HTML输入字段.
hidden
属性可以通过以下两种方式之一实现:- 在所需元素上的其他某些元素的 临时覆盖 .
- 在所需元素上的其他元素的 永久覆盖 .
- 属性的存在,例如
class="ng-hide"
,style="display: none"
等 - 根据最佳实践,在发送字符序列时,请勿尝试在任何
<p>
或<div>
标记上调用click()
或sendKeys()
,而应在命令后的所需<input>
标记上调用click()
. Webdriver的官方定位器策略.
- The element is hidden as modern JavaScript-centric UI styles always keep the ugly raw HTML input field hidden. The
hidden
attribute could have been implemented through either of the following ways:- A temporary overlay of some other element over the desired element.
- A permanent overlay of some other element over the desired element.
- Presence of attributes e.g.
class="ng-hide"
,style="display: none"
, etc - As per best practices while sending character sequence, you must not attempt to invoke
click()
orsendKeys()
on any<p>
or<div>
tag, instead invokeclick()
on the desired<input>
tag following the Official locator strategies for the webdriver.
有多种方法可以解决此问题.
There are different approaches to address this issue.
-
如果 临时覆盖 ,请使用 ExpectedConditions ,以使所需的元素可见/可点击,如下所示:
Incase of temporary overlay use WebDriverWait inconjunction with ExpectedConditions for the desired element to be visible/clickable as follows:
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.nsg-button"))).click();
-
如果 永久覆盖 ,请使用 JavascriptExecutor 接口,如下所示:
-
Incase of permanent overlay use
executeScript()
method from JavascriptExecutor interface as follows:import org.openqa.selenium.JavascriptExecutor; String inputText = "Rozmeen"; WebElement myElement = driver.findElement(By.id("u_0_b")); String js = "arguments[0].setAttribute('value','"+inputText+"')" ((JavascriptExecutor) driver).executeScript(js, myElement);
您将在如果存在属性,例如
class="ng-hide"
,style="display: none"
,等等,使用 JavascriptExecutor 界面来编辑和重置style="display: block"
的style="display: none"
属性,如下所示:Incase presence of attributes e.g.
class="ng-hide"
,style="display: none"
, etc useexecuteScript()
method from JavascriptExecutor interface to edit and reset thestyle="display: none"
attribute tostyle="display: block"
as follows:import org.openqa.selenium.JavascriptExecutor; ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");
您可以在可以填写隐藏的文字"区域元素
- For input[type=file], should allow sendKeys even when display=none
- Special-casing file upload controls in keyboard-interactability check
- Element is not reachable by keyboard
- Input field with display: none is not interactable at all?
如果您查看 Facebook 登录页面的 HTML ,该应用程序将包含 本地 元素.因此,在系统中下一次运行时,曾经用
id
表示为 u_0_b 的元素可能不会用与 u_0_b 相同的id
表示.因此,我们必须借助动态定位器策略.您可以使用以下代码块执行预期的步骤:If you look into the HTML of Facebook login page, the application contains React Native elements. So an element once represented with
id
as u_0_b in your system may not be represented by the sameid
as u_0_b in the next run on your system. Hence, we have to take help of Dynamic Locator Strategy. You can use the following code block to perform your intended steps :-
代码块:
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.facebook.com"); driver.findElement(By.xpath("//input[@name='firstname' and contains(@class,'inputtext')]")).sendKeys("testing it "); //DOB Select sel1 = new Select(driver.findElement(By.xpath(".//*[@id='month']"))); sel1.selectByIndex(4); Select sel2 = new Select(driver.findElement(By.xpath(".//*[@id='day']"))); sel2.selectByValue("6"); Select sel3 = new Select(driver.findElement(By.xpath(".//*[@id='year']"))); sel3.selectByValue("2013"); //clicking sign up driver.findElement(By.xpath("//button[@name='websubmit' and contains(.,'Sign Up')]")).click();
-
浏览器客户端:
-
Browser Client :
解决错误:
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
借助Firefox功能的可用性,变得更加轻松 moz:webdriverClick
Have become more easier with the availability of Firefox capability moz:webdriverClick
通过
webdriverClick()
,您可以传递一个布尔值,以指示在执行单击或向元素发送键时要运行哪种交互性检查.对于 v58.0 之前的 Firefoxen ,一些旧代码是从 WebDriver规范要求的交互性检查默认情况下启用.这意味着geckodriver将另外检查单击时某个元素是否被另一个元素遮挡,以及该元素是否可聚焦以发送键.由于这种行为上的变化,我们知道可能会返回一些额外的错误.在大多数情况下,可能需要更新所涉及的测试,以使其符合新的检查要求.Through
webdriverClick()
you can pass a boolean value to indicate which kind of interactability checks to run when performing a click or sending keys to an elements. For Firefoxen prior to v58.0 some legacy code as imported from an older version of FirefoxDriver was in use. With the availability of Firefox v58 the interactability checks as required by the WebDriver specification are enabled by default. This means geckodriver will additionally check if an element is obscured by another when clicking, and if an element is focusable for sending keys. Because of this change in behaviour, we are aware that some extra errors could be returned. In most cases the test in question might have to be updated so it's conform with the new checks.要暂时禁用WebDriver一致性检查,请使用
false
作为此功能的值.To temporarily disable the WebDriver conformant checks use
false
as value for this capability.注意:此功能仅暂时存在,并且在稳定交互性检查后将被删除.
Note: This capability exists only temporarily, and that it will be removed once the interactability checks have been stabilized.