如何勾选复选框是否启用?在selenium + Testng中,在应用程序im中单击复选框将被启用,我需要提前验证复选框是否已启用或not.thanks。
发布于 2014-11-10 15:26:31
有一个方法"isEnabled()",用于检查某个WebElement是否已启用或未启用。你可以使用下面的代码来检查它;
boolean enabled = driver.findElement(By.xpath("//xpath of the checkbox")).isEnabled();(我在上面使用过xpath,但您也可以使用id或cssselector来定位元素。)
如果启用了相关的WebElement,即复选框,上面的代码将返回‘’,否则将返回'false'。
如果你想让检查复选框是否被选中,你可以使用"isSelected()“方法,你可以这样使用它;
boolean checked = driver.findElement(By.xpath("//xpath of the checkbox")).isSelected();如果选中了相关的WebElement,则上面的代码将返回‘’,否则将返回'false'。
在评论中使用你的代码片段,我想出了一个如下的方法:
如果选中该复选框,则返回字符串"Pass“,如果未选中该复选框,则返回"Fail”,或者在执行此代码时出现错误时返回“Fail”。
public static String isCheckBoxChecked(String objlocator, String elemName) {
APP_LOGS.debug("Checking if the checkbox related to '"+elemName+"' is checked or not.");
System.out.println("Checking if the checkbox related to '"+elemName+"' is checked or not.");
try {
findWebElement(objlocator);
//Assuming the objLocator contains xpath
if (driver.findElement(By.xpath(objlocator)).isSelected()) {
System.out.println("Checkbox related to: '"+elemName+"' is checked.");
APP_LOGS.debug("Checkbox related to: '"+elemName+"' is checked.");
}else{
System.out.println("Checkbox related to: '"+elemName+"' is not checked!!");
APP_LOGS.debug("Checkbox related to: '"+elemName+"' is not checked!!");
return "Fail" + ": Checkbox related to: '"+elemName+"' is not checked!!";
}
}
catch (Throwable t) {
System.out.println("Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage());
APP_LOGS.error("Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage());
return "Fail"+": Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage();
}
return "Pass"+": Checkbox related to: '"+elemName+"' is checked.";
}发布于 2014-11-10 14:50:39
使用以下代码。这将根据复选框是否被选中来返回布尔值。在此基础上,您可以单击复选框。祝你编码愉快。
boolean selected = element.isSelected();
if(!selected)
{
element.click();
}发布于 2014-11-10 14:56:01
在以下情况下使用此选项:
bool flag = false;
if (checkBox1.Checked == true)
flag = true;https://stackoverflow.com/questions/26838153
复制相似问题