2014-07-11 2 views
0

웹 테이블의 열에있는 각 셀의 텍스트를 가져 오려고합니다. 나는이 방법에서 열 인덱스를 얻을 수 및이 방법에 매개 변수로 전달 : 여기 셀렌을 사용하여 웹 테이블의 열 셀 값 가져 오기

public List<String> getColumnValues(int colIndex) { 
     WebElement colElement; 
     List<String> colValues = new ArrayList<String>(); 
     List<WebElement> rows = getRows(); 
     System.out.println("Rows count: " + rows.size()); 
     Iterator<WebElement> i = rows.iterator(); 
     while (i.hasNext()) { 
      WebElement row = i.next(); 
      System.out.println("Row data: " + row.getText()); 
      // How to avoid this check for the header row for each row 
      // iteration? 
      if (row.findElements(By.tagName("th")).size() > 0) { 
       colElement = row.findElement(By.xpath(".//th[" + colIndex + "]")); 
      } else { 
       colElement = row.findElement(By.xpath(".//td[" + colIndex + "]")); 
      } 
      colValues.add(colElement.getText().trim()); 
     } 
     return colValues; 
    } 

내 웹 테이블 : 5 호선에

<table id = "webtable"> 
<thead> 
<tr> 
    <th>Header Column 1</th> 
    <th>Header Column 2</th> 
    <th>Header Column 3</th> 
    <th>Header Column 4</th> 
    <th>Header Column 5</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
<td>Row 1 Col 1</td> 
<td>Row 1 Col 2</td> 
<td>Row 1 Col 3</td> 
<td>Row 1 Col 4</td> 
<td>Row 1 Col 5</td> 
</tr> 
</tbody> 
<tbody> 
<tr> 
<td>Row 2 Col 1</td> 
<td>Row 2 Col 2</td> 
<td>Row 2 Col 3</td> 
<td>Row 2 Col 4</td> 
<td>Row 2 Col 5</td> 
</tr> 
</tbody> 
</table> 

문제 1. 나는 적절한 수를 얻을 행의. 그러나 9 행에서 행 (셀) 값을 선택하지 않습니다. 그것의 인쇄 공백. 행 1 열 1, 행 1 열 2, 행 1 열 3 등을 기다리고있었습니다.

문제 2 행 12에서 태그 "th"가 반환 된 요소의 수는 3이지만 5 여야합니다.

라인 (15)에 문제 3. 내가 언급 한 바와 같이 나는 [5]

문제 4. TD를 //까지 가져올 수 있지만 .//td[3] 찾으려고 이러한 요소 예외를 얻을 코멘트에서 행 요소의 각 반복에서 머리글 행에 대한 검사를 피할 수 있습니까?

누구든지 내가 잘못하고있는 것 또는 목록으로 반환 된 열 값을 얻는 더 좋은 방법이 있는지 설명하십시오. 내 테이블에 특정 값이 있는지 확인하려면 hasitem(item)을 사용하여 내 Hamcrest Matcher 어설 션에서 사용할 수 있도록 열 값으로 컬렉션이 필요합니다.

+0

'getRows()'의 기능은 무엇입니까? – SiKing

답변

1

이 기능을 사용하십시오. 그것은 당신이 색인으로 매개 변수로 색인을 제공 한 열의 값을 포함하는 목록을 반환합니다.

public List<String> getColumnValues(int colIndex) { 
WebElement colElement; 
List<String> colValues = new ArrayList<String>(); 

colValues = driver.findElements(By.cssSelector("#webtable>tbody>tr>td:nth-child("+colIndex+")")); 

return colValues; 
    } 

응답 어떤 문제가 발생하면 대답을 반환합니다.

+0

코드가 잘못되었습니다. 목록 (colValues) –

+0

에 목록 (driver.findElements)을 할당 할 수 없습니다. 으로 변경 한 다음 요소를 사용하여이 경우 colValues.getText() 메소드로 string을 반환해야합니다. – Deepak

관련 문제