【问题标题】:How can I check checkbox using selenium webdriver?如何使用 selenium webdriver 检查复选框?
【发布时间】:2014-05-13 20:28:36
【问题描述】:
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();
    }
}

【问题讨论】:

  • 只是模拟click,你得到了什么例外。只是一个建议:尽可能避免Thread().sleep
  • 问题出在哪里?代码会很慢,但应该做的事情
  • 这种类型的异常我得到了。在端口 28917 org.openqa.selenium.ElementNotVisibleException 上启动 ChromeDriver (v2.8.241075):元素不可见(会话信息:chrome=32.0.1700.102)(驱动程序信息:chromedriver=2.8.241075,平台=Windows NT 6.1 SP1 x86)(警告:服务器未提供任何堆栈跟踪信息)
  • 我错过了问题的重点。您已经在发布的代码中检查了复选框,那么您到底想做什么?
  • 当我运行脚本时,我在 Chrome 浏览器中发现以下异常错误。在端口 28917 org.openqa.selenium.ElementNotVisibleException 上启动 ChromeDriver (v2.8.241075):元素不可见(会话信息:chrome=32.0.1700.102)(驱动程序信息:chromedriver=2.8.241075,平台=Windows NT 6.1 SP1 x86)(警告:服务器未提供任何堆栈跟踪信息)

标签: java selenium checkbox selenium-webdriver


【解决方案1】:

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

简单的逻辑。

【讨论】:

    【解决方案2】:

    你确定这部分

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 2013-01-04
      • 2023-04-11
      • 2017-06-09
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多