gpt4 book ai didi

facebook - org.openqa.selenium.ElementNotInteractableException : Element is not reachable by keyboard: while sending text to FirstName field in Facebook

转载 作者:行者123 更新时间:2023-11-30 05:15:32 31 4
gpt4 key购买 nike

错误是:

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简而言之,这意味着无法使用键盘访问该元素,这意味着您甚至无法与它进行物理交互。

原因

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

  • 元素是 Conceal 因为现代以 JavaScript 为中心的 UI 样式总是 Conceal 丑陋的原始 HTML 输入字段。 hidden属性可以通过以下任一方式实现:
  • A 临时覆盖 在所需元素上的一些其他元素。
  • A 永久覆盖在所需元素上的一些其他元素。
  • 属性的存在,例如 class="ng-hide" , style="display: none"
  • 根据发送字符序列时的最佳实践,您不得尝试调用 click()sendKeys()在任何 <p><div>标记,而是调用 click()在所需的 <input>标签在 Official locator strategies for the webdriver 之后.

  • 解决方案

    有不同的方法来解决这个问题。
  • 万一临时覆盖使用 WebDriverWaitExpectedConditions 的结合使所需元素可见/可点击,如下所示:
    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();
  • 万一永久覆盖使用 executeScript() 方法来自 JavascriptExecutor界面如下:
    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);

    您会在 Using JS to enter text, but if I input text in one text box, the value already entered is getting deleted 中找到详细的讨论。
  • 如果存在属性,例如 class="ng-hide" , style="display: none" 等使用 executeScript() 方法来自 JavascriptExecutor界面编辑和重置 style="display: none" 归因于 style="display: block" 如下:
    import org.openqa.selenium.JavascriptExecutor;
    ((JavascriptExecutor) driver).executeScript("document.getElementById('ID').style.display='block';");

    您会在 Can't fill in the Hidden text area element 中找到详细的讨论。

  • 引用
  • 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,该应用程序包含 React Native元素。所以一个元素曾经用 id 表示如 u_0_b 在您的系统中可能不是由相同的 id 表示的如 u_0_b 下次在你的系统上运行。因此,我们必须借助动态定位器策略。您可以使用以下代码块来执行您的预期步骤:
  • 代码块:
    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();
  • 浏览器客户端:

    FacebookRegistration


  • 更新

    解决错误:
    org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard

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

    moz:webdriverClick()

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

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

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

    关于facebook - org.openqa.selenium.ElementNotInteractableException : Element is not reachable by keyboard: while sending text to FirstName field in Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49864965/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com