查找复选框并添加文本

时间:2015-06-07 19:44:04

标签: java selenium selenium-webdriver

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

我的硒方法如下所示:

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

有什么建议我做错了吗?

感谢您的回复!

2 个答案:

答案 0 :(得分:1)

这里的问题是textarea依赖于另一个按钮,您需要在使textarea可编辑之前执行单击。见下图。

//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

答案 1 :(得分:0)

以下代码应该有效 -

public void addSecondaryText(String string) {
    //click secondary button
    WebElement secButton = driver.findElement(By.xpath("//span[@class='sub-text-toggle']"));
    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);
}