org.openqa.selenium.elementNotInteractitableException:向Facebook中的FirstName字段发送文本时,键盘无法访问元素
错误是:org.openqa.selenium.elementNotInteractitableException:向Facebook中的FirstName字段发送文本时,键盘无法访问元素,facebook,selenium,react-native,selenium-webdriver,webdriver,Facebook,Selenium,React Native,Selenium Webdriver,Webdriver,错误是: Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <div id="u_0_b" class="_5dbb"> is not reachable by keyboard 您可以尝试以下代码: public class Rozmeen{ static WebDriver driver; static WebDriverWait wa
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <div id="u_0_b" class="_5dbb"> is not reachable by keyboard
您可以尝试以下代码:
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);
}
}
请尝试此代码并告诉我状态。ElementNotInteractiableException:键盘无法访问元素
元素无法通过键盘访问
简单地说,意味着无法使用键盘访问该元素,这意味着您甚至无法与它进行物理交互
理由
键盘无法访问错误元素的背后可能有多种原因,可能是以下原因之一:
- 元素是隐藏的,因为现代以JavaScript为中心的UI样式总是隐藏丑陋的原始HTML输入字段。
属性可以通过以下任一方式实现:hidden
- 在所需元素上的其他元素的临时覆盖
- 在所需元素上的某个其他元素的永久覆盖层
- 属性的存在,例如
,class=“ng hide”
等style=“display:none”
- 根据发送字符序列时的最佳实践,您不得尝试在任何
或
标记上调用
或click()
,而应在所需的sendKeys()
标记后调用click()
- 如果是临时覆盖请使用inconjunction with,以便所需元素可见/可点击,如下所示:
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();
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);
import org.openqa.selenium.JavascriptExecutor; ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");
- 如果永久覆盖使用以下界面方法:
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();
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);
您将在中找到详细的讨论import org.openqa.selenium.JavascriptExecutor; ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");
- 如果存在属性,例如
,class=“ng hide”
,请使用界面中的方法编辑属性,并将其重置为style=“display:none”
,如下所示:style=“display:none”
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();
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);
您将在中找到详细的讨论import org.openqa.selenium.JavascriptExecutor; ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");
这个问题 如果查看Facebook登录页面的HTML,则应用程序包含元素。因此,在系统中用
id
表示为u\u 0\u b的元素在下次运行时可能不会用与u\u 0\u 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()
通过webdriverClick()
可以传递一个布尔值,以指示在执行单击或向元素发送键时要运行哪种类型的可交互性检查。对于v58.0之前的Firefoxen,正在使用从较旧版本导入的一些旧代码。随着Firefox v58的可用性,默认情况下会启用所需的可交互性检查。这意味着geckodriver会在点击时额外检查一个元素是否被另一个元素遮挡,以及一个元素是否可用于发送键。由于这种行为的变化,我们意识到可能会返回一些额外的错误。在大多数情况下,有问题的测试可能必须更新,以使其符合新的检查
要临时禁用WebDriver一致性检查,请使用false
作为此功能的值
注意:此功能仅暂时存在,一旦可交互性检查稳定,它将被删除。在一个用例中,我遇到了相同的问题:
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <div id="search"> is not reachable by keyboard
测试后,我改为CSS选择器,它解决了这个问题:
driver.findElement(By.cssSelector("#search > input:nth-child(2)")).sendKeys("...");
因此,我强烈建议使用不同的方法与元素交互,因为其他方法可以节省您解决问题的时间。我遇到了类似的问题,在执行click()操作时,在按钮上显示org.openqa.selenium.elementNotInteractiableException异常。正如DebanjanB在回答中提到的那样,键盘无法访问元素。
要解决此问题,请将click()替换为sendKeys(Keys.ENTER)并单击按钮。错误状态为,
driver.findElement(By.id(“u\u 0\u b”)。sendKeys(“测试”)
看起来不可交互。一步一步地浏览你的脚本,看看它是否在显示,是否可以访问。是否有一个元素覆盖它?在第一步,它显示相同的错误。我已经一行一行地检查了每一行,但每次都可以看到相同的错误。我尝试使用wait命令,但仍然不起作用。嗨,chromedriver怎么样?我试过上面的webdriver.until(ExpectedConditions.elementtobelickable)、presentOfElement等,但运气不好。我也试过JS视图。也许禁用CSS会更容易些?临时或永久覆盖不会让你得到ElementNotInteractiableException。您将得到一条错误消息,其中提到试图单击X但Y元素挡住了去路。这个答案大部分是错误的或包含误导性信息。@JeffC我想你在这里有点困惑。我建议你看看浏览器的架构和CSS是如何工作的。这将消除你的基本疑虑。如果您在这些主题或方向上需要进一步帮助,请告诉我。不。。。一点也不困惑。当您设置此处描述的场景并亲自尝试时,您会发现它也不正确。否:按键盘键与单击鼠标按钮不同-t