如何使用selenium webdriver检查复选框?

时间:2014-04-02 13:44:19

标签: java selenium checkbox selenium-webdriver

public void AddIssues()
{
    try
    {
        getChromeDriver().findElementByClassName("odd").click();
        Thread.sleep(5000);
        getChromeDriver().manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        getChromeDriver().findElementById("view-issue").click();
        Thread.sleep(3000);
        getChromeDriver().findElementById("report-missing-doc-link").click();
        Thread.sleep(2000);
        getChromeDriver().findElement(By.id("1")).click();
        Thread.sleep(5000);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

你确定这个部分

       `getChromeDriver().findElement(By.id("1")).click();`

如果是,请等待元素显示然后单击。

while(true) {
    if(getChromeDriver().findElement(By.id("1")).isDisplayed())
        break;
}
getChromeDriver().findElement(By.id("1")).click();

答案 1 :(得分:0)

getting started with selenium project

中查看这些方法
/**
 * Check a checkbox, or radio button.
 * @param by The element to check.
 * @return
 */
public AutomationTest check(By by) {
    if (!isChecked(by)) {
        waitForElement(by).click();
        assertTrue(by.toString() + " did not check!", isChecked(by));
    }
    return this;
}
...
/**
 * Checks if the element is checked or not.
 * @param by
 * @return <i>this method is not meant to be used fluently.</i><br><br>
 * Returns <code>true</code> if the element is checked. and <code>false</code> if it's not.
 */
public boolean isChecked(By by) {
    return waitForElement(by).isSelected();
}

see the file here

您可以看到这将确保点击后检查它。

- Is the checkbox / radio button checked?
  If No: click()
  If Yes: return

简单的逻辑。