org.openqa.selenium.elementNotInteractiveException:键盘无法访问元素:向Faceb中的FirstName字段发送文本时

2024-10-03 00:25:54 发布

您现在位置:Python中文网/ 问答频道 /正文

错误是:

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

Tags: inidnewbydriver错误exceptionselect
3条回答

在其中一个用例中,我有同样的问题:

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

在发送键之前使用id标识元素。类似于:

driver.findElement(By.id("search")).sendKeys("...");

测试之后,我换成了CSS选择器,它解决了这个问题:

driver.findElement(By.cssSelector("#search > input:nth-child(2)")).sendKeys("...");

因此,我强烈建议使用不同的方法与元素交互,因为其他方法可以节省您解决问题的时间。

您可以尝试以下代码:

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

试试这个代码,让我知道状态。

ElementNotInteractiveException:键盘无法访问元素

Element is not reachable by keyboard用简单的词表示,使用键盘无法访问元素,这意味着您甚至不会与元素进行物理交互。

原因

键盘无法访问错误元素的背后可能有多种原因,这些原因可能是:

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

解决方案

有不同的方法来解决这个问题。

  • 在临时覆盖的情况下,使用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();
    
  • 对于永久覆盖,使用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"等,请使用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

    中找到详细的讨论

参考文献


这个问题

如果查看Facebook登录页面的HTML,应用程序将包含ReactNative元素。因此,在系统中曾经用id表示为u0ub的元素,在下次系统上运行时可能不会用与u0ub相同的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();
    
  • 浏览器客户端:

    FacebookRegistration


更新

解决错误:

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

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

moz:webdriverClick()

通过webdriverClick()可以传递一个布尔值,指示在执行单击或向元素发送键时要运行哪种交互检查。对于Firefoxenv58.0之前的版本,一些从较旧版本的FirefoxDriver导入的遗留代码正在使用中。使用Firefox v58的可用性,默认情况下,WebDriver specification所需的可交互性检查是启用的。这意味着geckodriver还将检查一个元素在单击时是否被另一个元素遮挡,以及一个元素是否可用于发送键。由于行为上的这种变化,我们知道可能会返回一些额外的错误。在大多数情况下,所讨论的测试可能需要更新,以便与新的检查一致。

要临时禁用符合WebDriver的检查,请使用此功能的值。

注意:此功能仅暂时存在,并且一旦稳定了可交互性检查,它将被删除。

相关问题 更多 >