【问题标题】:How to compare between two list simultaneously and perform conditional actions?如何同时比较两个列表并执行条件操作?
【发布时间】: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 语句 inside for 循环,但缩进与循环相同,这是不正确的。 --- 例如for 循环跟在正常语句之后,但缩进不同,这是不正确的。
  • 希望现在更好。感谢您的提示。
  • 你的预期是如果复选框checked和参数false,那么取消选中?

标签: java excel selenium selenium-webdriver apache-poi


【解决方案1】:

如果比较多个条件,则使用&amp;&amp; 运算符,并使用方法.isSelected() 来检查元素是否被选中。试试下面这个:

for (int i = 0; i < checkboxes.size() ; i++) {
    if(checkboxes.get(i).isSelected() && colstatuslist.get(i).toUpperCase().equals("FALSE")){
        checkboxes.get(i).click();
    } else if((!checkboxes.get(i).isSelected()) && colstatuslist.get(i).toUpperCase().equals("TRUE")){
        checkboxes.get(i).click();
    }
}

【讨论】:

  • 这很好用,没想到我可以使用isSelected()。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 2016-11-14
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多