简体   繁体   English

Selenium Webdriver:基于多个行值查找复选框

[英]Selenium Webdriver: finding checkbox based on multiple row values

I went through this site but could not find a solution for my problem and was wondering if people with more experience in Selenium could help me out. 我浏览了该站点,但是找不到解决我问题的方法,并且想知道在Selenium方面有更多经验的人是否可以帮助我。 Apologies for the long post, but I wanted to make sure that I explained things properly. 很长的道歉,但是我想确保我正确地解释了事情。

I am using Java. 我正在使用Java。 I normally use Rational Function Tester (Java) but I am trying to see if I can rewrite the code from some of the tests, to see how easy it would be to convert. 我通常使用Rational Function Tester(Java),但我试图查看是否可以重写某些测试中的代码,以了解转换的难易程度。

I have a table with 11 columns and 4 rows (but it could be 20 or 50 rows, it fluctuates during testing). 我有一个包含11列和4行的表(但它可能是20或50行,在测试过程中会波动)。 I can read the table and its content to find the row that I want. 我可以阅读表及其内容来查找所需的行。 I compare each of the first 10 cell values and, if they all match, I want to click on the checkbox that is in column 11. Otherwise, continue to the next row. 我将比较前10个单元格值中的每个值,如果它们都匹配,我想单击第11列中的复选框。否则,请继续下一行。

The function works properly to retrieve the values from each cell. 该函数可以正常工作以从每个单元格中检索值。 However, when I try to click on the checkbox, it is always selecting the one that is on the first row. 但是,当我尝试单击复选框时,总是选择第一行上的复选框。 When I checked the attributes, I can see that all the checkboxes have the same 'name' but a different value (such as 1330361, 1330363, 1330359, etc.). 当我检查属性时,我可以看到所有复选框都具有相同的“名称”但值不同(例如1330361、1330363、1330359等)。 Interestingly, if I do a search for all the check boxes in the row, it reports 4 of them (the number found in the whole table, not on the row). 有趣的是,如果我搜索该行中的所有复选框,它将报告其中的4个(在整个表中找到的数字,而不是在行中)。

I am probably making a very basic mistake but I cannot figure out what it is. 我可能犯了一个非常基本的错误,但我无法弄清楚它是什么。

I use the following code to search in the table. 我使用以下代码在表中进行搜索。 The function receives the table row and, at this point, I am just reporting the cell values then attempt to click on the checkbox that is in the row. 该函数接收表行,此时,我只是报告单元格值,然后尝试单击该行中的复选框。 It is kind of a debugging function. 这是一种调试功能。 I am not sure if it is proper to post a huge amount of code, so I will just put a short version of it, where I try to click on each of the checkbox in the table. 我不确定是否发布大量代码是否合适,所以我将其放一个简短的版本,在其中尝试单击表中的每个复选框。

// 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();
     }

The XPath to get the checkbox has the whole page as scope. 用于获取复选框的XPath将整个页面作为范围。 I would add a dot in front to get a scope relative to the element: 我会在前面添加一个点以获得相对于元素的作用域:

// 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();
  }
}

But a better way would be to gather all the checkboxes before iterating the columns: 但是更好的方法是在迭代列之前收集所有复选框:

// 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();
  }
}

If you want to Click check box based on values, you have to list all values using a for loop and Check box absolute xpath and pass "i" value, See code below: 如果要单击基于值的复选框,则必须使用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);
            }




        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM