【问题标题】:Selenium Webdriver: finding checkbox based on multiple row valuesSelenium Webdriver:根据多行值查找复选框
【发布时间】:2016-07-15 03:54:11
【问题描述】:

我浏览了这个网站,但找不到解决我的问题的方法,我想知道是否有更多 Selenium 经验的人可以帮助我。 为这篇长文道歉,但我想确保我正确地解释了事情。

我正在使用 Java。我通常使用 Rational Function Tester (Java),但我想看看是否可以重写一些测试的代码,看看转换起来有多容易。

我有一个包含 11 列和 4 行的表(但它可能是 20 或 50 行,它在测试期间会波动)。 我可以阅读表格及其内容来找到我想要的行。我比较前 10 个单元格值中的每一个,如果它们都匹配,我想单击第 11 列中的复选框。否则,继续下一行。

该函数可以正常工作以从每个单元格中检索值。但是,当我尝试单击复选框时,它总是选择第一行的那个。 当我检查属性时,我可以看到所有复选框都有相同的“名称”但值不同(例如 1330361、1330363、1330359 等)。 有趣的是,如果我搜索行中的所有复选框,它会报告其中的 4 个(在整个表中找到的数字,而不是在行中)。

我可能犯了一个非常基本的错误,但我无法弄清楚它是什么。

我使用以下代码在表格中进行搜索。该函数接收表格行,此时,我只是报告单元格值,然后尝试单击该行中的复选框。这是一种调试功能。 我不确定发布大量代码是否合适,所以我只放一个简短的版本,我尝试点击表格中的每个复选框。

// Passing a specific table row to the function:
List<WebElement> columns=myRow.findElements(By.tagName("td"));
for (int iLoop = 0;iLoop < columns.size();iLoop++)
{
     System.out.println("Column " + iLoop + " : '" + columns.get(iLoop).getText().toString() + "'");
     // code to compare actual and expected values and setting of a boolean variable.

     if (iLoop == 10 && recordMatch)
     {
          tCheckbox = myRow.findElement(By.xpath("//input[@type='checkbox']"));
         System.out.println("Value is :" + tCheckbox.getAttribute("value").toString());
          tCheckbox.click();
     }

【问题讨论】:

    标签: java selenium checkbox webdriver


    【解决方案1】:

    如果要根据值单击复选框,则必须使用 for 循环和复选框绝对 xpath 列出所有值并传递“i”值, 见以下代码:

            WebElement table = driver.findElement(By.xpath("//div[@id ='content']/table"));
            /*List <WebElement> head = driver.findElements(By.tagName("th"));
            head.size();
    
            for (int h=0;h<head.size();h++){*/
            List <WebElement> row = driver.findElements(By.tagName("tr"));
            row.size();
    
    
            for (int i=0;i<row.size();i++){
    
    
    
                List <WebElement> col = row.get(i).findElements(By.tagName("td"));
                col.size();
    
    
                for (int j=0;j<col.size();j++){
                    String cv = col.get(j).getText();
    // If name is Saudi Arabia, it will click on check box 
                    if(cv.equalsIgnoreCase("Saudi Arabia")){
    // Divide the x path where colunm is changing html/body/div[1]/div[5]/div[2]/div/div/table/tbody/tr[pass i value]/td[6]";
                        String xp1 = "html/body/div[1]/div[5]/div[2]/div/div/table/tbody/tr[";
                        String xp2 = "]/td[6]";
    
    // Pass the i value xp1+i+xp2
                        driver.findElement(By.xpath(xp1+i+xp2)).click();
                    }
                    System.out.println(cv);
                }
    
    
    
    
            }
    

    【讨论】:

      【解决方案2】:

      获取复选框的 XPath 将整个页面作为范围。我会在前面添加一个点以获得相对于元素的范围:

      // Passing a specific table row to the function:
      List<WebElement> columns=myRow.findElements(By.tagName("td"));
      for (int iLoop = 0;iLoop < columns.size();iLoop++)
      {
        System.out.println("Column " + iLoop + " : '" + columns.get(iLoop).getText().toString() + "'");
        // code to compare actual and expected values and setting of a boolean variable.
      
        if (iLoop == 10 && recordMatch)
        {
          tCheckbox = myRow.findElement(By.xpath(".//input[@type='checkbox']"));
          System.out.println("Value is :" + tCheckbox.getAttribute("value").toString());
          tCheckbox.click();
        }
      }
      

      但更好的方法是在迭代列之前收集所有复选框:

      // Passing a specific table row to the function:
      List<WebElement> columns=myRow.findElements(By.xpath("//td[.//input[@type='checkbox']]"));
      List<WebElement> checkboxes=myRow.findElements(By.xpath("//td//input[@type='checkbox']"));
      for (int iLoop = 0;iLoop < columns.size();iLoop++)
      {
        System.out.println("Column " + iLoop + " : '" + columns.get(iLoop).getText().toString() + "'");
        // code to compare actual and expected values and setting of a boolean variable.
      
        if (iLoop == 10 && recordMatch)
        {
          tCheckbox = checkboxes.get(iLoop);
          System.out.println("Value is :" + tCheckbox.getAttribute("value").toString());
          tCheckbox.click();
        }
      }
      

      【讨论】:

      • 嗨弗洛伦特,哇!那效果很好。我非常感谢你。我想我真的需要学习 xpath 知识。出于某种原因,它只是没有陷入困境。我已经习惯了使用 RFT。安德烈
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 2019-02-06
      • 2014-05-29
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多