java - 找到复选框并添加文本

标签 java selenium selenium-webdriver

我想在此 website 处添加辅助文本字段文本字符串。

我的 Selenium 方法如下所示:

public void addSecondaryText(String string) {
    //click secondary button
    WebElement secButton = driver.findElement(By.xpath("//label/input[@id='sub-text-check']"));
    if (secButton.isDisplayed()) {
        secButton.click();          
    }
    //clear text
    WebElement secTextField = driver.findElement(By.xpath("//*[@id='sub-text']"));
    secTextField.clear(); // I get the exception here!
    //add text
    secTextField.sendKeys(string);
}

但是,我目前遇到 secTextField.clear(); 异常,无法对其进行操作:

Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state: Element is not currently interactable and may not be manipulated

有什么建议我做错了什么吗?

感谢您的回复!

最佳答案

这里的问题是文本区域依赖于另一个按钮,您需要先单击它才能使文本区域可编辑。请参见下图。

//List will help of to determine the "Add secondary text" button needs to be clicked on not
//if the count is greater than 0 then click it
IList<IWebElement> elements = driver.FindElements(By.XPath("//span[contains(text(),'Add secondary text')]"));
if (elements.Count>0)
{
    elements.FirstOrDefault().Click();
}
IWebElement element = driver.FindElement(By.Id("sub-text"));
element.Clear();
element.SendKeys("Test");

enter image description here

关于java - 找到复选框并添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30697747/

相关文章:

java - 从 Hibernate hbm 到 JPA 注释,一个具有挑战性的任务

javascript - 如何使用 Selenium 和 Java 接受 "This action cannot be undone. Are you sure you want to delete this wishlist"弹出窗口

javascript - Google 59 不再支持嵌入式凭据

java - 在 WebDriver 中使用稳健的 Try Catch 方法?

python - 如何在 Windows XP 上使用 pip 安装 selenium 包?

java - Shiro session 注销不起作用

java - 如何在Java正则表达式中分割这个 "Tree-like"字符串?

java - Java 7 下的 JAXB 问题它说找不到 RI 2.1.0

java - 撰写邮件点击在 Selenium 中不起作用

Python Selenium 超时移至循环中的下一项