【问题标题】:How to resolve ElementNotInteractableException thrown by sendKeys method of Selenium WebDriver?如何解决 Selenium WebDriver 的 sendKeys 方法抛出的 ElementNotInteractableException?
【发布时间】:2018-06-27 20:12:15
【问题描述】:

作为学习 Selenium WebDriver 的练习,我正在尝试访问 Twitter log in page 并登录。我终于能够导航到该页面并为用户名字段获取所需的*元素:

<input class="text-input email-input js-signin-email" name="session[username_or_email]" type="text">

*我试图找到 sendKeys 使用哪些类型的元素,但我找不到。我认为这个元素是我想要的,因为标签是<input> 类型。

当我在分配元素后调用sendKeys 时,我得到一个ElementNotInteractableException。我尝试使用显式等待与ExpectedConditions 的visibilityOf 和invisibilityOf。 TimeoutException 和 ElementNotInteractableException 的各自解释告诉我,我上面提到的元素加载到 DOM 树中但永远不会变得可见*。我该如何解决这个问题?有没有使用JavascriptExecutor 的解决方案?

*周围的 html 可能会揭示为什么元素仍然不可见:

<form class="t1-form clearfix signin js-signin"
action="https://twitter.com/sessions" method="post">
  ::before
  <fieldset>
    <legend class="visuallyhidden">Log in</legend>
    <div class="clearfix field">
      ::before
      <input class="text-input email-input js-signin-email"
      name="session[username_or_email]" type="text">

.

**对于那些正在寻找的人,this 问题有助于查看,特别是如果问题是由 click 方法引起的。

【问题讨论】:

    标签: java html selenium selenium-webdriver sendkeys


    【解决方案1】:

    您可以使用此代码:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Test  {
      public static void main(String[] args) throws InterruptedException {
        final WebDriver driver = new ChromeDriver();
    
        driver.get("https://twitter.com/login?lang=en");
    
        WebDriverWait wait = new WebDriverWait(driver, 10);
    
        // locate login input
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@placeholder = 'Phone, email or username']")));
        WebElement loginInput = driver.findElement(By.xpath("//input[@placeholder = 'Phone, email or username']"));
        loginInput.click();
        loginInput.sendKeys("login");
    
        // locate password input
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//fieldset//input[@placeholder = 'Password']")));
        WebElement passInput = driver.findElement(By.xpath("//fieldset//input[@placeholder = 'Password']")); // this xPath does the trick
        passInput.click();
        passInput.sendKeys("password");
        Thread.sleep(3000); // only to see the result
    
        //locate submit button
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@type= 'submit']")));
        WebElement submitBtn = driver.findElement(By.xpath("//button[@type= 'submit']"));
        submitBtn.click();
    
        Thread.sleep(3000); // only to see the result
        driver.quit();
      }
    }
    

    【讨论】:

    • 在sendKeys 之前使用click 是一种好习惯/必要吗?
    • 最后,JavascriptExecutor 的使用成本更高和/或最后的手段?
    • 原来我试图使用By.name 定位并且有多个。 By.xpath 至高无上。谢谢。
    • sendKeys前必须使用click。 JavascriptExecutor 也是可以的,而且不贵,但是如果你可以用 Java 做一些简单的事情,那么使用 JavascriptExecutor 是没有意义的。
    【解决方案2】:

    编辑:显然我没有做足够的研究,虽然我没有在 StackOverflow 上看到这个问题。如果它对某人有帮助,请留下这个问题。

    使用JavascriptExecutor(来源 1.):

    1. 导入:import org.openqa.selenium.JavascriptExecutor;
    2. 总体思路:JavascriptExecutor executor= (JavascriptExecutor) driver; executor.executeScript(script, arguments);
    3. 对于sendKeys 替代方案,我们需要执行一些 JS,使用点表示法设置元素的文本值。我们必须先找到元素。 (来源 1。)使用 document.getElementById('id')、document.getElementsByTagName('name') 或 document.getElementsByClassName('class') 查找元素(来源 2。)。请注意,对于用于定位元素的最后两个选项,会收集多个元素,因此请索引您想要的那个。

    原来是我的解决方案的示例代码行: executor.executeScript("document.getElementsByClassName('js-username-field email-input js-initial-focus')[0].value='someValue';");

    请注意,.getElementById('id') 可能是首选,但在我的情况下,网站设计师决定不合作并且不提供 id 属性。

    这里有两个(令人难以置信的!)关于这个主题的资源——看看它们!

    1. https://www.softwaretestingmaterial.com/javascriptexecutor-selenium-webdriver/
    2. https://www.w3schools.com/js/js_htmldom_document.asp

    【讨论】:

    • 在你的情况下,JavascriptExecutor 不需要,只需使用我提供的xPath 即可。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2021-01-08
    • 2018-11-23
    • 2014-09-01
    • 2012-10-15
    相关资源
    最近更新 更多