如何保存已选中复选框的状态并在页面加载后使用该值。 使用下面提到的代码,我单击了一个复选框,但是在单击“计算”按钮后,我的页面加载并且该复选框未被选中。
这是我的代码:
driver.findElement(By.id("F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2")).click();
这是复选框的HTML表单
INPUT onclick="document.all('hdnIsValueChanged').value=1; if (F1372B03C0090002_01082008__ctl2_HiddenBox_F1372B03C0090002.value==0) {F1372B03C0090002_01082008__ctl2_HiddenBox_F1372B03C0090002.value=1;};" id=F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2 CHECKED type=checkbox value="" name=F1372B03C0090002_01082008:_ctl2:F1372B03C0090002_01082008C2
答案 0 :(得分:0)
在单击“计算”按钮之前,您是否试图将其值存储在变量中?
重新加载后,您可以为其分配适当的值。
答案 1 :(得分:0)
请使用下面的代码片段检查复选框的状态,然后单击它。
WebElement checkBox1 = driver.findElement(By.id("F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2"));
boolean checkboxState = checkBox1.isSelected();
if (checkBox1.isSelected() == true) {
System.out.println("Check box is selected");
} else {
System.out.println("Check box is not selected");
checkBox1.click();
}
答案 2 :(得分:0)
您应先将值存储在Map中,然后再单击Calculate,创建自定义单击,以便将项目添加到地图,这是一个简单的示例:
private HashMap<String, String> values;
// this method will click on the element(checking it) and use store method
public void checkAndStore(final WebElement element) {
Actions action = new Actions(driver);
action.moveToElement(element).click(element).build().perform();
addToMap(element);
}
// here we store the id of element as key to the map and value as value
private void addToMap(WebElement element) {
values.put(element.getAttribute("id"), element.getAttribute("value"));
}
// this is your getter
public HashMap<String, String> getMyMap() {
return this.values;
}
// and now you can click as many checkbox as you want and get the result aftewards:
@Test
void myTest() throws Exception {
checkAndStore(driver.findElement(By.id("1")));
checkAndStore(driver.findElement(By.id("2")));
checkAndStore(driver.findElement(By.id("3")));
driver.findElement(By.id("myCalculateButton")).click();
System.out.println(getMyMap().entrySet());
}