Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在selenium中加载页面后保存复选框值_Java_Selenium_Checkbox - Fatal编程技术网

Java 如何在selenium中加载页面后保存复选框值

Java 如何在selenium中加载页面后保存复选框值,java,selenium,checkbox,Java,Selenium,Checkbox,如何保存选中复选框的状态并在页面加载后使用该值。 使用下面提到的代码,我单击了一个复选框,但单击“计算”按钮后,我的页面将加载,复选框将被取消选中 这是我的代码: driver.findElement(By.id("F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2")).click(); 这是复选框的Html表单 INPUT onclick="document.all('hdnIsValueChanged').value=1

如何保存选中复选框的状态并在页面加载后使用该值。 使用下面提到的代码,我单击了一个复选框,但单击“计算”按钮后,我的页面将加载,复选框将被取消选中

这是我的代码:

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

在单击“计算”按钮之前,您是否尝试将其值存储在变量中


重新加载后,您可以为其正确分配值

在单击“计算”按钮之前,您是否尝试将其值存储在变量中


重新加载后,您可以为其正确分配值

请使用下面的代码片段检查复选框的状态并单击它

WebElement checkBox1=driver.findElement(按.id(“F1372B03C0090002_01082008___ctl2_F1372B03C0090002_01082008C2”);
布尔checkboxState=checkBox1.isSelected();
if(checkBox1.isSelected()==true){
System.out.println(“选中复选框”);
}否则{
System.out.println(“未选中复选框”);
复选框1.单击();

}
请使用下面的代码片段检查复选框的状态并单击它

WebElement checkBox1=driver.findElement(按.id(“F1372B03C0090002_01082008___ctl2_F1372B03C0090002_01082008C2”);
布尔checkboxState=checkBox1.isSelected();
if(checkBox1.isSelected()==true){
System.out.println(“选中复选框”);
}否则{
System.out.println(“未选中复选框”);
复选框1.单击();

}
在单击“计算”之前,应将值存储在地图中,创建自定义单击,以便将项目添加到地图中,下面是一个简单的示例:

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());
}
私有HashMap值;
//此方法将单击元素(检查它)并使用store方法
公共无效检查和存储(最终WebElement){
动作动作=新动作(驱动);
action.moveToElement(元素)。单击(元素)。构建()。执行();
addToMap(元素);
}
//在这里,我们将元素的id存储为映射的键,将值存储为值
私有void addToMap(WebElement元素){
value.put(element.getAttribute(“id”)、element.getAttribute(“value”);
}
//这是你的救星
公共HashMap getMyMap(){
返回这个值;
}
//现在,您可以单击任意多个复选框,并在以后获得结果:
@试验
void myTest()引发异常{
checkAndStore(driver.findElement(By.id)(“1”));
checkAndStore(driver.findElement(By.id(“2”)));
checkAndStore(driver.findElement(By.id)(“3”));
driver.findElement(By.id(“myCalculateButton”)。单击();
System.out.println(getMyMap().entrySet());
}

在单击“计算”之前,应将值存储在地图中,创建自定义单击,以便将项目添加到地图中,下面是一个简单的示例:

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());
}
私有HashMap值;
//此方法将单击元素(检查它)并使用store方法
公共无效检查和存储(最终WebElement){
动作动作=新动作(驱动);
action.moveToElement(元素)。单击(元素)。构建()。执行();
addToMap(元素);
}
//在这里,我们将元素的id存储为映射的键,将值存储为值
私有void addToMap(WebElement元素){
value.put(element.getAttribute(“id”)、element.getAttribute(“value”);
}
//这是你的救星
公共HashMap getMyMap(){
返回这个值;
}
//现在,您可以单击任意多个复选框,并在以后获得结果:
@试验
void myTest()引发异常{
checkAndStore(driver.findElement(By.id)(“1”));
checkAndStore(driver.findElement(By.id(“2”)));
checkAndStore(driver.findElement(By.id)(“3”));
driver.findElement(By.id(“myCalculateButton”)。单击();
System.out.println(getMyMap().entrySet());
}

不,即使将值存储在变量中,我也会尝试不工作。WebElement复选框=driver.findElement(按.id(“F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2”);复选框。单击();布尔值=diver.findElement(按.id(“F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2”))。isSelected();不,我会尝试在将值存储在变量中后仍不起作用。WebElement复选框=driver.findElement(按.id(“F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2”);复选框。单击();布尔值=diver.findElement(按.id(“F1372B03C0090002_01082008__ctl2_F1372B03C0090002_01082008C2”))。isSelected();我试过这个…运气不好。当我点击Calculate时,复选框被取消选中…我如何使用cookies或本地存储来保存复选框的状态,以便它在页面加载后获取值我尝试了这个…没有运气。当我点击Calculate时,复选框被取消选中…我如何使用cookies或本地存储来保存复选框的状态,以便在页面加载后它获取值