【发布时间】:2019-12-27 10:00:20
【问题描述】:
我正在将网页复选框状态与 excel 文件中设置的参数进行比较。但是,它卡在无休止地打开和关闭复选框。
我尝试了以下代码。
我从 excel 中获取数据进行比较的方式。
HSSFSheet dispcolsheet = workbook2.getSheet("Display Columns");
DataFormatter df = new DataFormatter();
Iterator<Row> colRowItr = dispcolsheet.rowIterator();
List<String> colstatuslist = new ArrayList<String>();
while (colRowItr.hasNext()){
Row row = colRowItr.next();
Cell colname = row.getCell(0);
if (colname.getStringCellValue().startsWith("chkColumns_")) {
Cell colstatuscell = row.getCell(1);
String colstatus = df.formatCellValue(colstatuscell);
colstatuslist.add(colstatus);
}
}
试用 1
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[id^=chkColumns_]"));
for (int i = 0; i < checkboxes.size() ; i++) {
for (int j = 0; j < colstatuslist.size() ; j++)
if(checkboxes.get(i).getAttribute("checked type") !=null){
if(colstatuslist.get(j).equals("FALSE")){
checkboxes.get(i).click();
}
}
else {
if(colstatuslist.get(j).equals("TRUE")){
checkboxes.get(i).click();
}
}
}
试用 2
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[id^=chkColumns_]"));
for (int i = 0; i < checkboxes.size() ; i++) {
if(checkboxes.get(i).getAttribute("checked type") !=null){
for (int j = i; j < colstatuslist.size() ; j++) {
if (colstatuslist.get(j).equals("FALSE")){
checkboxes.get(i).click();
}
}
}
else {
for (int j = i; j < colstatuslist.size() ; j++) {
if (colstatuslist.get(j).equals("TRUE")){
checkboxes.get(i).click();
}
}
}
}
这两个代码只会打开和关闭第一个复选框。
试用 3
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[id^=chkColumns_]"));
Iterator<String> collistitr = colstatuslist.iterator();
Iterator<WebElement> chkboxitr = checkboxes.iterator();
while (collistitr.hasNext() && chkboxitr.hasNext()) {
for (int i = 0; i < checkboxes.size() ; i++) {
if(checkboxes.get(i).getAttribute("checked type") !=null){
if(colstatuslist.get(i).equals("No")){
checkboxes.get(i).click();
}
}
else {
if(colstatuslist.get(i).equals("Yes")){
checkboxes.get(i).click();
}
}
}
}
此代码将无休止地一一点击所有复选框。
关于excel部分的上下文,你可以访问我的另一个线程:Why I cannot get values from excel into a list using my codes? 我希望在选中 checkbox1 并且 excel 值为“False”时实现,它应单击 checkbox1 取消选中,反之亦然以匹配 excel 参数。喜欢:
On web|check status|On xls |xls parameter
box0 get(i) checked get(j) False ->click box0 on web to uncheck, next
box1 get(i+1) not check get(j+1) True ->click box1 on web to check, next
box2 get(i+2) checked get(j+2) True ->do nothing, compare next
box3 get(i+3) not check get(j+3) False ->do nothing, compare next
...
【问题讨论】:
-
您不正确的缩进使代码不必要地难以阅读。请修复它。
-
对不起,但我是编码新手,所以我不确定什么是合适的缩进,特别是我正在处理许多循环,我也让自己变得凌乱......
-
例如
if语句 insidefor循环,但缩进与循环相同,这是不正确的。 --- 例如for循环跟在正常语句之后,但缩进不同,这是不正确的。 -
希望现在更好。感谢您的提示。
-
你的预期是如果复选框
checked和参数false,那么取消选中?
标签: java excel selenium selenium-webdriver apache-poi