org.openqa.selenium.ElementNotInteractableException:键盘无法访问元素:在Facebook中将文本发送到FirstName字段时
react-native selenium-webdriver (2)
ElementNotInteractableException:键盘无法访问元素
Element is not reachable by keyboard
简单的单词
Element is not reachable by keyboard
意味着使用键盘
Element is not reachable by keyboard
元素,这意味着您甚至无法与其进行物理交互。
原因
错误背后可能有多种原因 键盘无法访问元素 ,可以是以下任一种情况:
-
该元素是
隐藏的,
因为现代的以JavaScript为中心的UI样式始终隐藏着丑陋的原始HTML输入字段。
hidden
属性可以通过以下任一方式实现:- 在所需元素上 临时叠加 一些其他元素。
- 在所需元素上 永久叠加 其他元素。
-
存在属性,例如
class="ng-hide"
,style="display: none"
等 -
根据发送字符序列时的最佳做法,您不得尝试在任何
<p>
或<div>
标记上调用click()
或sendKeys()
,而是在 官方定位器策略 之后调用所需<input>
标记上的click()
对于webdriver 。
解
有不同的方法来解决这个问题。
-
包含 临时覆盖 使用 WebDriverWait 与 ExpectedConditions 以使所需 元素可见/可点击 ,如下所示:
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 接口的
executeScript()
方法,如下所示: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);
您将在 使用JS输入文本中 找到详细讨论 ,但如果我在一个文本框中输入文本,则已输入的值将被删除
-
如果出现属性,例如
class="ng-hide"
,style="display: none"
等,请使用 JavascriptExecutor 界面中的executeScript()
方法编辑并将style="display: none"
属性重置为style="display: block"
如下:import org.openqa.selenium.JavascriptExecutor; ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");
您将在 无法填写隐藏文本区域元素中 找到详细讨论
参考
- 对于input [type = file],即使display = none也应该允许sendKeys
- 键盘交互性检查中的特殊外壳文件上载控件
- 键盘无法访问元素
- 带显示的输入字段:none根本不可交互?
这个特殊问题
如果您查看
Facebook
登录页面的
HTML
,该应用程序包含
React
Native
元素。
因此,在您的系统中,一旦用
id
作为
u_0_b
表示的元素可能不会在系统的下一次运行中由与
u_0_b
相同的
id
表示。
因此,我们必须采取
动态定位策略的
帮助。
您可以使用以下代码块执行预期的步骤:
-
代码块:
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();
-
浏览器客户端
更新
解决错误:
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
随着Firefox功能的可用性变得更加容易 moz:webdriverClick
MOZ:webdriverClick()
通过
webdriverClick()
您可以传递一个布尔值,以指示在执行单击或向元素发送键时要运行哪种交互性检查。
对于
v58.0
之前的
Firefoxen
,从旧版本的
FirefoxDriver
导入的一些遗留代码正在使用中。
随着
Firefox v58
的推出,默认情况下会启用
WebDriver规范
所要求的交互性检查。
这意味着geckodriver将另外检查一个元素在点击时是否被另一个元素遮挡,以及一个元素是否可以用于发送键。
由于行为的这种变化,我们意识到可能会返回一些额外的错误。
在大多数情况下,可能必须更新有问题的测试,以使其符合新的检查。
要临时禁用WebDriver一致性检查,请使用
false
作为此功能的值。
注意 :此功能仅暂时存在,并且一旦交互性检查已稳定,它将被删除。
错误是:
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();
你可以试试这段 代码 :
public class Rozmeen{
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "F:\\Automation\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 40);
driver.get("http://www.facebook.com");
//entering first name
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("pagelet_bluebar"))));
driver.findElement(By.name("firstname")).sendKeys("testing it ");
//DOB
selectFromDropDown(driver.findElement(By.name("birthday_day")), "4");
selectFromDropDown(driver.findElement(By.name("birthday_month")), "Jun");
selectFromDropDown(driver.findElement(By.name("birthday_year")), "2013");
//clicking sign up
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("websubmit"))));
driver.findElement(By.name("websubmit")).click();
}
public static void selectFromDropDown(WebElement element , String Visibletext){
Select select = new Select(element);
select.selectByVisibleText(Visibletext);
}
}
试试这段代码,让我知道状态。