如何使用Selenium WebDriver读取表中的单元格值 [英] How to read cell values in a table using selenium webdriver
问题描述
[下面是html代码.实际上,我无法理解如何真正解释Webtable具有两个表ID(tbl和tbr)以及该表共有4行的情况.表id = tbl下的第一列以及其余的Rev和Qty和我正在尝试检查col值是否存在或不考虑每一行,例如,表id = tbl下的col1是零件名称,表id = tbr下的col2和col3是否具有零件修订和零件数量.我想检查零件是否存在指定的修订和数量.我的问题现在清楚了吗?
[Below is the html code. Actually I am unable to understand how to actually explain the condition the webtable has two table ids, tbl and tbr and the table has a total of 4 rows.The first column in under the table id=tbl and the remaining columns Rev and Qty and others under the table id=tbr.I am trying to check the col values exists or not considering each row for example col1 under table id=tbl is a Part Name and col2 and col3 under table id=tbr has Part Revision and Part Quantity. I want to check if the Part exists with the Revision and Quantity specified.Is my question clear now?
<form autocomplete="off" onsubmit="return false;" name="tvcTableForm">
<input type="hidden" value="tvc-0000000000000002" name="object">
<input type="hidden" value="35004.36845.30464.63080" name="parentOID">
<div id="th">
<div id="tb">
<div id="tblc" style="top: 32px; height: 112px;">
<table id="tbl" class="contentTable" cellspacing="0" cellpadding="0" style="border: 0px none; width: 199px; height: 144px; table-layout: fixed;">
<tbody>
</table>
</div>
<div id="tbrc" style="top: 32px; left: 199px; width: 1106px; height: 112px; overflow: auto;">
<table id="tbr" class="contentTable" cellspacing="0" cellpadding="0" style="border: 0px none; width: 1089px; height: 144px; table-layout: fixed;">
<tbody>
<tr id="r573378134017" class="even" oncontextmenu="return ctxm(event)" style="height: 36px;">
推荐答案
这是使用硒和java
语言获取Web表每个单元格值的方法.
This is the way to get the value of each cell of a web table using selenium with java
language.
1.表初始化
在您的情况下,表的ID为tbr
,这是初始化它的方式:
In your case, the table has the id tbr
, the way to initialize it :
WebElement tbl = driver.findElement(By.id("tbr"));
2.行初始化
Web表格行的名称标签通常为tr
,这是初始化它的方式:
The name tag for the web table row in general is tr
, the way to initialize it :
List<WebElement> rows = tbl.findElements(By.tagName("tr"));
3.列初始化
Web表格列的名称标签通常为th
或td
,这是初始化它的方式:
The name tag for the web table column in general are th
or td
, the way to initialize it :
th 标签
List<WebElement> cols = rows.get(rowIndex).findElements(By.tagName("th"));
td 标签
List<WebElement> cols = rows.get(rowIndex).findElements(By.tagName("td"));
因此,您可以通过以下方式获取特定的单元格值:
String cell = cols.get(indexCol).getText();
以下代码是获取所有值单元格表的功能(不获取标题th
值)
The bellow code is function to get all value cell table (without getting header th
value)
WebElement tbl = driver.findElement(By.id("tbr"));
//check all row, identification with 'tr' tag
List<WebElement> rows = tbl.findElements(By.tagName("tr"));
//row iteration
for(int i=0; i<rows.size(); i++) {
//check column each in row, identification with 'td' tag
List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));
//column iteration
for(int j=0; j<cols.size(); j++) {
System.out.println(cols.get(j).getText());
}
//This is to get the cell value you want
////get col no 2
//System.out.println(cols.get(1).getText());
////get col no 8
//System.out.println(cols.get(7).getText());
}
这篇关于如何使用Selenium WebDriver读取表中的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!