1

I need to select a column value only if value of column 1 and 7 matches in a specific row of a table. the number of rows is dynamic and has 8 columns.

This is my code :

    WebElement session_table = driver.findElement(By.id("sessions_table"));
    List<WebElement> session_row = driver.findElements(By.tagName("tr"));
    System.out.println(session_row.get(2).findElement(By.xpath("//td[2]")).getText());
    Iterator<WebElement> i= session_row.iterator();
    while(i.hasNext())
    {
        WebElement srow= i.next();
        List<WebElement> session_data = srow.findElements(By.xpath("//td"));
        stime = session_data.get(0).getText();
    coach_name =  session_data.get(6).getText();
        System.out.println( stime + "    " + coach_name);
       if((stime == "11/02/12 07:30 AM") && (coach_name == "Test Coach1")  )
        {
            driver.findElement(By.xpath("(//a[contains(text(),'0/4')])")).click();

        }


    }

But the variables stime and coach_name are assigned the value of first row. it does not change with iteration. For example if first row has value " 07:00 AM " and coach name Test and there are 80 rows, it prints "07:00 AM Test" 80 times. but i need to read value of each row. Note : i use selenium webdriver with java

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nithya
  • 41
  • 1
  • 2
  • 5
  • My main concern is i need to read data from each row. and if value in column1 and column7 of a row matches a criteria i need to click a link in column 8. – Nithya Nov 02 '12 at 14:50
  • I found the answer to my question in the following link http://stackoverflow.com/questions/7180243/selenium-2-can-findelementby-xpath-be-scoped-to-a-particular-element – Nithya Nov 14 '12 at 09:41
  • Possible duplicate of [Selenium 2 - Can findElement(By.xpath) be scoped to a particular element?](http://stackoverflow.com/questions/7180243/selenium-2-can-findelementby-xpath-be-scoped-to-a-particular-element) – Brian Tompsett - 汤莱恩 Apr 26 '17 at 21:10

1 Answers1

1

In order to compare String objects you need to use equals or compareTo methods, not ==. With == you are comparing object references.

dan
  • 13,132
  • 3
  • 38
  • 49