Selenium webdriver 复选框已选中,但可以';无法使用selenium单击它

Selenium webdriver 复选框已选中,但可以';无法使用selenium单击它,selenium-webdriver,Selenium Webdriver,单击复选框时,复选框将高亮显示,但未单击 我也没有例外 <input name="include_notice" onclick="javascript:TogglePublishDates();" type="checkbox"> 我用名称标识此复选框,并尝试使用sendkeysReturn和sendKeysEnter 注意:此测试用例在相当长的一段时间内运行良好。Selenium Web驱动程序或Firefox中未做任何更改。您可以尝试单击使用java脚本,如下所示: J

单击复选框时,复选框将高亮显示,但未单击 我也没有例外

<input name="include_notice" onclick="javascript:TogglePublishDates();" type="checkbox">


我用名称标识此复选框,并尝试使用
sendkeysReturn
sendKeysEnter


注意:此测试用例在相当长的一段时间内运行良好。Selenium Web驱动程序或Firefox中未做任何更改。

您可以尝试单击使用java脚本,如下所示:

JavascriptExecutor e = (JavascriptExecutor)wd;
e.executeScript("arguments[0].click();", driver.findElement(By.name("include_notice")));
    //1) Finding the check box
    WebElement checkBox = driver.findElement(By.name("include_notice"));
    //2) Checking whether check box is already checked
    if (!checkBox.isSelected()) {
        JavascriptExecutor e = (JavascriptExecutor)wd;
        e.executeScript("arguments[0].click();", checkBox);
        //3) Checking whether first attempt to check the check box worked
        if (!checkBox.isSelected()) {
            //4) Retrying
            checkBox.click();
        }
    }
如果仍然观察到不一致的行为,则可能需要实现重试机制,如下所示:

JavascriptExecutor e = (JavascriptExecutor)wd;
e.executeScript("arguments[0].click();", driver.findElement(By.name("include_notice")));
    //1) Finding the check box
    WebElement checkBox = driver.findElement(By.name("include_notice"));
    //2) Checking whether check box is already checked
    if (!checkBox.isSelected()) {
        JavascriptExecutor e = (JavascriptExecutor)wd;
        e.executeScript("arguments[0].click();", checkBox);
        //3) Checking whether first attempt to check the check box worked
        if (!checkBox.isSelected()) {
            //4) Retrying
            checkBox.click();
        }
    }

如果您还有任何疑问,请告诉我。

您可以尝试使用以下代码:

driver.findElement(By.name("include_notice")).click();  //find checkbox element and click on it.
单击使用java脚本执行器的复选框

WebElement checkbox = driver.findElement(By.name("include_notice"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", checkbox);
如果已选中复选框,则使用此代码

 WebElement checkbox =  driver.findElement(By.name("include_notice"));
 if (!checkBox.isSelected())        //checkbox is not selected then only it will select the checkbox.
 {
     checkBox.click();
     System.out.println(checkbox.isSelected());
 }

请尝试单击两次。另外,您是否可以确保您唯一地标识此特定元素?我对代码
的解释对您有帮助吗?如果确实如此,请将此答案标记为
已接受