2016-03-28 120 views
0

獲取單元格的值這裏是我的一塊HTML的Java。硒webdriver。從表

<table class="table_results data ajax" data-uniqueId="20605"> 
    <thead> 
    </thead> 
    <tbody> 
    <tr class="odd"> 
    <td data-decimals="0" data-type="int" class="right data not_null nowrap">1</td> 
    <td data-decimals="0" data-type="string" data-originallength="5" class="data not_null text ">user1</td> 
    <td data-decimals="0" data-type="string" data-originallength="40" class="data not_null text ">e38ad214943daad1d64c102faec29de4afe9da3d</td> 
    <td data-decimals="0" data-type="string" data-originallength="14" class="data not_null text ">[email protected]</td> 
    <td data-decimals="0" data-type="string" data-originallength="6" class="data not_null text ">Smith</td> 
    <td data-decimals="0" data-type="string" data-originallength="1" class="data not_null text ">&quot;</td> 
    </tr> 
    </tbody> 
</table> 

所以我需要的是檢索單元格的值。 還有一件事是,當通過瀏覽器的源代碼選項查看代碼時,它看起來與上面相同。但使用Firefox的螢火,當另外表明,價值觀是

<span></span> 

標籤

裏面我知道這裏XPath是解決方案。在過去的2個小時裏,我嘗試了很多,但都沒有成功。 當然,我也看到類似的問題的解決方案,但不知何故,他們不適合我。

我想要做的事:

WebElement table_element = driver.findElement(By.xpath("//table//tbody")); 
    ArrayList<WebElement> rows = (ArrayList<WebElement>) table_element.findElements(By.tagName("tr")); 
    for (WebElement row : rows) { 
     ArrayList<WebElement> cells = (ArrayList<WebElement>) row.findElements(By.tagName("//td")); 
     for (WebElement cell : cells) { 
      System.out.println(cell.getText()); 
     } 
    } 
+0

提供'xpath'你試過和單元格的值你想得到 – Andersson

+0

對不起,不能粘貼在這裏,所以它填充格式正確的方式。我將它添加到問題 – Kleeo

回答

0

找到td元素,並調用.getText()方法:

List<WebElement> rows = driver.findElements(By.cssSelector("table.table_results tr")); 
for (WebElement row: rows) { 
    List<WebElement> cells = row.findElements(By.cssSelector("td.data")); 
    for (WebElement cell: cells) { 
     System.out.println(cell.getText()); 
    } 
} 

在這裏,我們使用CSS選擇查找表中的行,然後爲每個表格行排列單元格。

+0

在第一行中返回空列表。所以它不起作用。我用我的檢索方式得到同樣的問題。 – Kleeo

+0

也許你的桌子在iframe裏面。在這種情況下,它將檢索一個空列表。 –

0
List<WebElement> TRCollection = driver.findElement(By.className("table_results data ajax")).findElements(By.tagName("tr")); 

for (WebElement tr : TRCollection) 
{ 
    List<WebElement> TDCollection = tr.findElements(By.tagName("td")); 
    for (WebElement td: TDCollection) 
    { 
     System.out.println(td.getText()); 
    } 
} 

嘗試找到自己的表:

List<WebElement> tables = driver.findElements(By.tagName("table")); 

調試和找到你的表之後,你可以做上面的代碼:

例如像...

List<WebElement> TRCollection = tables[2].findElements(By.tagName("tr")); 
+0

我也試過這種方法。獲取 'org.openqa.selenium.InvalidSelectorException。複合類名稱不允許' – Kleeo

+0

看看其他框架中的表格 –

+0

如何檢查它? – Kleeo

0

爲了補充以前的awnser,如果需要從特定行中獲取文本,可以這樣做:

string userName = "user1"; 
WebElement row = driver.findElement(By.xPath("//table[contains(@class, 'table_results')]/tbody/tr/td[2][text()='" + userName + "']/..")); 

List<WebElement> cells = row.findElements(By.cssSelector("td.data")); 
for (WebElement cell: cells) { 
    System.out.println(cell.getText()); 
} 

在這種情況下,我使用td[2]來獲取行的第二列並比較用戶名。如果願望比較其他領域,你可以改變它。

此外,您可以如果XPath選擇工作斷言或如果它返回所有要使用Chrome控制檯:

$x("your xpath selector here") 
0
/** 
* match a string to the text of a table cell and return that row. 
* 
* @param table the table the data is in 
* @param cellTextEquals the text to identify the row e.g. unique ID 
* @param intCellToFind the column index in which to look for that text 
* @return table row 
*/ 
protected WebElement getRowFromTable(WebElement table, 
     String cellTextEquals, int intCellToFind) { 
    explicitWaitForElementToBeVisibleAndClickable(table); 
    WebElement tableBody = table.findElement(By.tagName("tbody")); 
    List<WebElement> rows = tableBody.findElements(By.tagName("tr")); 
    for (WebElement row : rows) { 
     List<WebElement> td = row.findElements(By.tagName("td")); 
     if (td.size() > 0 
       && td.get(intCellToFind).getText().equals(cellTextEquals)) { 
      return row; 
     } 
    } 
    return null; 
} 

/** 
* match a string to the text of a cell and return the text of 
* another specified cell in that row 
* 
* @param table the table the data is in 
* @param cellTextEquals the text to identify the row e.g. unique ID 
* @param intCellToFind the column index in which to look for that text 
* @param intCellToReturn the column index of the cell text you want to return 
* @return text of a specific cell in an html table 
*/ 
protected String getTextFromTableCell(WebElement table, String cellTextEquals, 
     int intCellToFind, int intCellToReturn) { 
    WebElement row = getRowFromTable(table, cellTextEquals, intCellToFind); 
    List<WebElement> td = row.findElements(By.tagName("td")); 
    if (td.get(intCellToFind).getText().equals(cellTextEquals)) { 
     return td.get(intCellToReturn).getText(); 
    } 
    return null; 
} 

/** 
* Gets an element from a table, either button or tagName, from a table. 
* 
* @param table the table the data is in 
* @param cellTextEquals the text to identify the row e.g. unique ID 
* @param intCellToFind the column index in which to look for that text 
* @param intCellToReturn the column index of the cell where the element is you want to return 
* @param buttonText the button text if it's a button element you want 
* @param tagName the tag name of the element i=icon, a=link etc. 
* @return Element By.tagName or button 
*/ 
protected WebElement getElementFromTableCell(WebElement table, 
              String cellTextEquals, int intCellToFind, int intCellToReturn, 
              String buttonText, String tagName) { 
    WebElement row = getRowFromTable(table, cellTextEquals, intCellToFind); 
    if (row != null) { 
     List<WebElement> td = row.findElements(By.tagName("td")); 
     if (td.get(intCellToFind).getText().equals(cellTextEquals)) { 
      if (!buttonText.equals("")) { 
       return td.get(intCellToReturn).findElement(By.xpath(
         "//button[contains(text(),'" + buttonText + "')]")); 
      } else { 
       return td.get(intCellToReturn) 
         .findElement(By.tagName(tagName)); 
      } 
     } 
    } 
    return null; 
}