org.openqa.selenium.ElementNotInteractableException:无法通过键盘访问元素:将文本发送到 Facebook 中的 FirstName 字段时 [英] org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard: while sending text to FirstName field in Facebook

查看:29
本文介绍了org.openqa.selenium.ElementNotInteractableException:无法通过键盘访问元素:将文本发送到 Facebook 中的 FirstName 字段时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是:

线程main"org.openqa.selenium.ElementNotInteractableException 中的异常:元素 

无法通过键盘访问

代码是:

System.setProperty("webdriver.gecko.driver","//Users//rozali//Documents//Selenium//geckodriver");WebDriver 驱动程序 = 新的 FirefoxDriver();driver.get("http://www.facebook.com");driver.manage().window().maximize();//输入名字driver.findElement(By.id("u_0_b")).click();driver.findElement(By.id("u_0_b")).sendKeys("testing it");//DOBSelect 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");//点击注册driver.findElement(By.id("u_0_t")).click();

解决方案

ElementNotInteractableException: 元素无法通过键盘访问

Element is notreachable by keyboard 简单来说,这意味着无法使用键盘访问该元素,这意味着您甚至不会与它进行物理交互.

原因

错误背后可能有多种原因键盘无法访问元素,可能是以下任一原因:

  • 元素被隐藏,因为现代以 JavaScript 为中心的 UI 样式总是隐藏丑陋的原始 HTML 输入字段.hidden 属性可以通过以下任一方式实现:
    • 其他元素在所需元素上的临时叠加.
    • 其他元素在所需元素上的永久叠加.
    • 属性的存在,例如class="ng-hide"style="display: none"
    • 根据发送字符序列时的最佳实践,您不得尝试在任何 <p> 上调用 click()sendKeys()code> 或

      标记,而是在

    <小时>

    更新

    解决错误:

    org.openqa.selenium.ElementNotInteractableException:键盘无法访问元素

    Firefox 功能的可用性变得更加容易moz:webdriverClick

    moz:webdriverClick()

    通过 webdriverClick(),您可以传递一个布尔值来指示在执行点击或向元素发送键时要运行的交互性检查类型.对于 v58.0 之前的 Firefoxen 一些从旧版本 FirefoxDriver 正在使用中.随着 Firefox v58 的推出,WebDriver 规范 要求进行交互性检查默认情况下启用.这意味着 geckodriver 将额外检查一个元素在单击时是否被另一个元素遮挡,以及一个元素是否可聚焦以发送键.由于这种行为变化,我们知道可能会返回一些额外的错误.在大多数情况下,有问题的测试可能需要更新,以符合新的检查.

    要暂时禁用 WebDriver 一致性检查,请使用 false 作为此功能的值.

    注意:此功能只是暂时存在,一旦交互性检查稳定,它将被删除.

    The error is :

    Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <div id="u_0_b" class="_5dbb"> is not reachable by keyboard
    

    The code is:

    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

    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.

    Reason

    There can be multiple reasons behind the error Element is not reachable by keyboard which can be either of the following:

    • 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() or sendKeys() on any <p> or <div> tag, instead invoke click() on the desired <input> tag following the Official locator strategies for the webdriver.

    Solution

    There are different approaches to address this issue.

    • 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();
      

    • 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);
      

      You will find a detailed discussion in Using JS to enter text, but if I input text in one text box, the value already entered is getting deleted

    • Incase presence of attributes e.g. class="ng-hide", style="display: none", etc use executeScript() method from JavascriptExecutor interface to edit and reset the style="display: none" attribute to style="display: block" as follows:

      import org.openqa.selenium.JavascriptExecutor;
      ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");
      

      You will find a detailed discussion in Can't fill in the Hidden text area element

    References


    This particular issue

    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 same id 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 :


    Update

    Addressing the error:

    org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
    

    Have become more easier with the availability of Firefox capability moz:webdriverClick

    moz:webdriverClick()

    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.

    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.

    这篇关于org.openqa.selenium.ElementNotInteractableException:无法通过键盘访问元素:将文本发送到 Facebook 中的 FirstName 字段时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆