当这样的表没有如下所示的正确html时,我们可以使用selenium吗?
下面是我用来获取表行元素的表,该表行元素具有特定元素,例如文本中包含“Harvest”的href,并检查同一表行中是否存在文本“running”当这样的表没有如下所示的正确html时,我们可以使用selenium吗?,selenium,Selenium,下面是我用来获取表行元素的表,该表行元素具有特定元素,例如文本中包含“Harvest”的href,并检查同一表行中是否存在文本“running” <table id="execTable" class="tableHistory jobtable translucent"> <colgroup> <col class="execid"> <col class="titlecol"> </colgroup&g
<table id="execTable" class="tableHistory jobtable translucent">
<colgroup>
<col class="execid">
<col class="titlecol">
</colgroup>
<tbody>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</tbody>
<tr id="8571">
<td>8571</td>
<td class="titlecol">
<div id="hitdiv-8571" class="arrow"></div>
<a href="/job?id=Harvest&logs">Harvest</a>
</td>
<td>09-03-2015 09:45:04</td>
<td>-</td>
<td>2m 6s</td>
<td>running</td>
<td><a href="flow?action=restart&id=8571">view/restart</a></td>
</tr>
<tr id="8571-child" class="childRow" style="display: none;"></tr>
<tr id="8566">
<td>8566</td>
<td class="titlecol">
<div id="hitdiv-8566" class="arrow"></div>
<a href="/job?id=mk trained input&logs">mk</a>
</td>
<td>09-03-2015 03:30:00</td>
<td>09-03-2015 04:16:50</td>
<td>46m 50s</td>
<td>succeeded</td>
<td><a href="/flow?action=restart&id=8566">view/restart</a></td>
</tr>
<tr id="8555-child" class="childRow" style="display: none;"></tr>
</table>
List all=driver.findElements(By.xpath(“./*[@id='execTable']/*”);
for(WebElement a:all){
if(a.getTagName().equalsIgnoreCase(“tr”){..}
}
我能够让上面的代码正常工作。谢谢大家! 当您尝试运行代码时会发生什么?请编辑您的问题,包括您的结果是什么,您尝试了什么,如果有的话,除了您在这里发布的内容。另外,您可以使用不同的定位器找到元素,或者选择所有tr并对其进行过滤(虽然可能不是最有效的方法)。我最后使用了下面的代码作为
List all=driver.findElements(by.xpath(“./*[@id='execTable']/*”);对于(WebElement a:all){…}
当您发布代码块时,请在每行的前面放置4个空格,而不是使用坟墓标记。墓碑是一个句子中的代码。这使它更容易阅读。当你运行代码时会发生什么?您没有得到TR
s以外的TBODY
?
WebElement table = driver.findElement(By.id("execTable"));
List<WebElement> trows = table.findElements(By.tagName("tr"));
List<WebElement> all = driver.findElements(By.xpath(".//*[@id='execTable']/*"));
for (WebElement a : all) {
if(a.getTagName().equalsIgnoreCase("tr")) { ....}
}