2

I am trying to find a row in a table by an element, then find a element in that row and return an attribute (Or do something else).

The java selenium code here finds a table body. The getText() accurately prints out the text in the row for each row, but the s.findElement(xpath).getText() will return the unique_cell_name whether the row contains it or not. And the if statement will be entered every loop.

How do I fix it? And why does the getText() return the correct text but I can't find elements in that same rowElement?

      List<WebElement> listOfRows2 = driver().findElement(By.xpath("//table/tbody")).waitUntilPresent().findElements(By.xpath("./tr"));
      for(WebElement rowElement : listOfRows2){
          System.out.println("getText: " + rowElement.getText());
          String name = rowElement.findElement(By.xpath("//a[contains(text(),'unique_cell_name')]")).getText();
          System.out.println("name : " + tooltip);
          if(name.equals('unique_cell_name')){
                rowElement.findElement(By.xpath("//a[contains(text(),'unique_cell_name_to_row_but_not_table')]")).doSomething();
                //also want to check text in a specific column 5, where the same text might exist in the same row in the different column
                rowElement.findElement(By.xpath("//td[2]")).doSomething();
                break;
          }
      }

I am using selenium 2.53 in java with serenity/cucumber.

JumpyToad
  • 21
  • 3
  • What exactly are you trying to do in `String name = rowElement.findElement(By.xpath("//a[contains(text(),'unique_cell_name')]")).getText();` ? Search a node through text and then extract the same text ? – undetected Selenium Feb 17 '18 at 05:24
  • Hard to figure out exact xpath code without html, but when you are trying to find an element inside another element, using xpath, then you need to prefix the xpath with a '.' So use By.xpath(".//td[2]") – Grasshopper Feb 17 '18 at 06:57
  • @DebanjanB That just stores the string which gets compared in the if statement to check if that that cell has the text in it, then in the if statement I will do something else to that same cell. For my actual use case, that element might be a button that I need to click or I need to get the tooltip from the element. – JumpyToad Feb 19 '18 at 14:17
  • @Grasshopper thanks! that worked – JumpyToad Feb 19 '18 at 15:10

1 Answers1

0

The issue was the period in By.xpath(".//td[2]") mentioned by GrassHopper to search a node's children

  List<WebElement> listOfRows2 = driver().findElement(By.xpath("//table/tbody")).waitUntilPresent().findElements(By.xpath("./tr"));
  for(WebElement rowElement : listOfRows2){
      System.out.println("getText: " + rowElement.getText());
      //This if statement is to check whether the row has the element or not, otherwise it will throw an error
      if(rowElement.findElements(By.xpath(".//a[contains(text(),'unique_cell_name')]").size() !=0){
        String name = rowElement.findElement(By.xpath(".//a[contains(text(),'unique_cell_name')]")).getText();
        System.out.println("name : " + name);
        if(name.equals('unique_cell_name')){
            rowElement.findElement(By.xpath(".//a[contains(text(),'unique_cell_name_to_row_but_not_table')]")).doSomething();
            //also want to check text in a specific column 5, where the same text might exist in the same row in the different column
            rowElement.findElement(By.xpath(".//td[2]")).doSomething();
            break;
        }
      }
      else{
        System.out.println("This is not the row you are looking for");
      }
  }
JumpyToad
  • 21
  • 3