【问题标题】:Getting ElementNotInteractableException error in Selenium Java在 Selenium Java 中获取 ElementNotInteractableException 错误
【发布时间】:2019-06-10 17:33:47
【问题描述】:

当我尝试通过从 Excel 工作表中获取数据来将文本输入到文本字段时出现此错误。

Error Message : Error while entering the text in DOM! Element <input id="Name" class="flipperMozEdit" name="Name" type="text"> is not reachable by keyboard
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z'
System info: host: 'G5CG7133385E', ip: '10.181.210.46', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_212'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\502457528\AppData\Local\Temp\rust_mozprofile.6Sn2RalXsr0R, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=XP, moz:accessibilityChecks=false, moz:useNonSpecCompliantPointerOrigin=false, acceptInsecureCerts=true, browserVersion=62.0.3, platformVersion=10.0, moz:processID=14160, browserName=firefox, javascriptEnabled=true, platformName=XP, moz:webdriverClick=true}]
Session ID: 1ea9a987-f329-464f-adac-af2b92064a1d

我已尝试以下代码: 元素

    <input type="text" value="" name="Name" id="Name" onfocus="FullSearch.displayDynamicTextarea(this)" size="30%" delimiter="|" class="flipperMozView">

    @FindBy(xpath = ".//*[@id='Name']")
    public WebElement txt_NameMR;


    BaseClass.waitForObj(10000);
    BaseClass.set(txt_NameMR,ExcelReport.testData.get("ownerName")+"*");
    BaseClass.waitForObj(5000);
    BaseClass.click(btn_Srch_Name);

这是 txt_NameMR 的元素

<input type="text" value="" name="Name" id="Name" onfocus="FullSearch.displayDynamicTextarea(this)" size="30%" delimiter="|" class="flipperMozView">

【问题讨论】:

  • 你没有展示足够的代码让我能够制作任何有用的 cmets,我要问的问题太多了。您正在使用@FindBy,这意味着 pagefactory,但我没有看到该代码,我根本没有看到 BaseClass,不知道显示的类是什么,等等。

标签: java selenium selenium-webdriver


【解决方案1】:

检查元素是否难以处理。 txt_NameMR.click(); // 这通常会激活 DOM 中的元素。

  BaseClass.waitForObj(10000);
     txt_NameMR.click();   BaseClass.set(txt_NameMR,ExcelReport.testData.get("ownerName")+"*");
        BaseClass.waitForObj(5000);
        BaseClass.click(btn_Srch_Name);

【讨论】:

  • 即使在提供 .click(); 之后我仍然遇到同样的错误;
  • org.openqa.selenium.ElementNotInteractableException:元素 无法通过键盘访问构建信息:版本: '3.5.3',修订:'a88d25fe6b',时间:'2017-08-29T12:54:15.039Z'
  • @jyothisharma 我猜该元素要么被禁用,要么被刷新。你能分享一下html页面源吗? ?我们可以看看有没有其他方式可以和文本框交互。
【解决方案2】:

我将根据提供的信息进行猜测。我假设您正在尝试使用 Java PageFactory 实现来使用页面对象,所以我认为您需要以下内容。

页面对象

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class MyPageObject {
    @FindBy(id = "Name")
    WebElement txt_NameMR;

    @FindBy(id = "<ID_OF_Search_Button>")
    WebElement btn_Srch_Name;

    WebDriverWait wait;

    public MyPageObject(WebDriver driver) {
        PageFactory.initElements(driver, this);
        wait = new WebDriverWait(driver, 15, 100);
    }

    public void searchFor(String text){
        wait.until(ExpectedConditions.elementToBeClickable(txt_NameMR)).sendKeys(text);
        wait.until(ExpectedConditions.elementToBeClickable(btn_Srch_Name)).click();
    }
}

主代码(调用页面对象)

MyPageObject myPageObject = new MyPageObject(driver);
myPageObject.searchFor(ExcelReport.testData.get("ownerName")+"*");

现在您可能仍然在此处遇到等待问题,页面对象将一直等到一个元素可点击,但由于显式等待是在一个 WebElement 中,如果您在页面上没有该元素时触发显式等待,您将看到NoSuchElement 异常。您可以通过更具针对性的显式等待来解决此问题,如下所示:

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Name")));

虽然没有简单的方法从 Java PageFactory WebElement 中获取定位器(按对象),但您有一些代码重复。这是我创建 Query library 的原因之一,它可以帮助您解决此类问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 2021-04-27
    • 2021-01-08
    • 2021-08-21
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多