0

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. Apologies for the long post, but I wanted to make sure that I explained things properly.

I am using 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.

I have a table with 11 columns and 4 rows (but it could be 20 or 50 rows, it fluctuates during testing). 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.

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.). 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).

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

2 Answers 2

1

The XPath to get the checkbox has the whole page as scope. 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();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Florent, Wow! That worked great. I thank you so much. I guess that I really need to work on the xpath knowledge. For some reason, it is just no sinking in. I am so used to working with RFT. Andre
0

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:

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




        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.