Html代码
<table class="adminlist" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr data-bind="if: RowCounts>0, attr: {StatusId: Id, DescName:Name}" statusid="2" descname="Abandoned"></tr>
<tr data-bind="if: RowCounts>0, attr: {StatusId: Id, DescName:Name}" statusid="1" descname="Active">
<td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="1">
</td>
<td class="country">
<span data-bind="text:Name">Active</span>
(
<span data-bind="text:RowCounts">20</span>
)
</td>
</tr>
我们怎样才能找到&#34; td&#34;元素是否存在与上表,基于上面的第一行没有td元素但第二行它有一个td元素
java代码
Hashtable<String, Integer> renewalStatus = new Hashtable<String, Integer>();
List<WebElement> activeAssetStatus = driver.findElements(By
.xpath("//*[@id='divPatPortfolioStatusCount']/table/tbody/tr"));
for (int i = 1; i <= activeAssetStatus.size(); i++) {
List<WebElement> statusValue = activeAssetStatus.get(i)
.findElements(By.tagName("td"));
if (statusValue.size() > 0) {
WebElement al = driver
.findElement(By
.xpath("//*[@id='divPatPortfolioStatusCount']/table/tbody/tr["
+ i + "]/td[2]"));
String mainText = al.getText();
final Pattern p = Pattern.compile("(.*?)\\s*\\((.*?)\\)");
final Matcher m = p.matcher(mainText);
m.find();
String name = m.group(1);
String count = m.group(2);
renewalStatus.put(name, Integer.parseInt(count));
}
}
}
以上代码无法在哈希表中收集和存储td值 它的产品&#34; NoSuchElementException&#34; 因为在if条件中我的值为1所以它产生了这个异常,我怎么能用td值收集相应的行
答案 0 :(得分:0)
List <WebElement> allTrElements = driver.findElements(By.xpath("//table[@class='adminlist']/tbody/tr"));
for(int i= 0;i<allTrElements.size();i++){
List<WebElement> allTdElements = allTrElements.get(i).findElements(By.tagName("td"));
if(allTdElements.size() > 0 ){
// if confirms <tr> contains <td>
}
}
答案 1 :(得分:0)
WebDriver mainDriver = new ChromeDriver();
//get the table element
WebElement eleTable = mainDriver.findElement(By.tagName("table");
//get the list of row elements of table
List<WebElement>tableRowList = eleTable.findElements(By.tagName("tr"));
for(int i = 0 ; i < tableRowList.size(); i++) {
//take individual rows
WebElement rowElement = tableRowList.get(i);
//get the list of td elements
List<WebElement> dataElement = rowElement.findElements(By.tagName("td"));
if(dataElement.size() > 0) {
//if size > 0, there are td elements in it.
}
}