2014-10-27 56 views
0

每个我有一个表被strutured像:入门的表

<Table> 
<tr class="x1"> 
<th>test</th> 
<td>...</td> 
<td>...</td> 
<td>...</td> 
<td>...</td> 
</tr> 
<tr class="x2"> 
<th>test2</th> 
<td>...</td> 
<td>...</td> 
<td>...</td> 
<td>...</td> 
</tr> 
<tr class="x3"> 
<th>test3</th> 
<td>...</td> 
<td>...</td> 
<td>...</td> 
<td>...</td> 
</tr> 
</table> 

我如何取值从<th>
我的想法是:创建一个可以在桌子上跑来跑去的每个<th>
可能吗?任何人有任何想法,我怎么能做到这一点使用硒的webdriver(JAVA)

我给这家 ArrayList<String> tableList = new ArrayList<String>(); for(int i = 1; i<=NUMBEROFROWSINYOURTABLE;i++){ tableList.add(driver.findElement(By.xpath("//div[@id='heatmap-container']/table/tbody/tr["+i+"]/th")).getAttribute("title")); }

的解决方案,但你可以按照@AndyPerfect的答案的例子太多,:)

+0

通常情况下,所有的''标签的走在第一个'' – 2014-10-27 16:49:49

+0

是的,但在这种情况下,每Tr有TH – 2014-10-27 17:04:06

回答

0

下面的例子在Python中,但它应该给你一个如何去做的想法。基本上,通过任何可用的方法获取表格元素。然后,在该元素上,调用findElements搜索标签名称为“th”的所有元素。这将给你一个在该特定表内的所有th元素的列表。然后,只需遍历元素并打印它们的值或其他值。

table_element = driver.find_element_by_id("tableid") 
th_elements = table_element.find_elements_by_tag_name("th") 
for element in th_elements: 
    print element.text 

在Java中,它会是这个样子

WebElement tableElement = driver.findElement(By.id("tableid")); 
List<WebElement> thElements = tableElement.findElements(By.tagName("th")); 
for (int i = 0; i < thElements.size(); i++) { 
    System.out.println(thElements.get(i).getText()); 
} 
+0

以下是JAVA列表中的代码 tableElement = driver.findElements(By.id(“tableid”)); 列表 thElements = tableElement.findElements(By.tagName(“th”));对于(int i = 0; i 2014-10-27 17:16:52

+0

感谢您的支持,Rishi。我会更新答案 – AndyPerfect 2014-10-27 17:18:25

+0

不客气安迪 – 2014-10-27 17:19:27