如何在xpath
中实现或调整条件td
中的文字?
从下表中,我的预期输出应为:
Black bean sauce
Black bean chicken
USA(New York)
India(New Delhi)
<table>
<tr class="foods foods_s-fndds2-f41205100">
<td class="name">
<a href="Javascript://">Black bean sauce</a>
</td>
<td class="calories">280</td>
</tr>
<tr class="foods foods_s-fndds2-f41205140">
<td class="name">
<a href="Javascript://">Black bean chicken</a>
</td>
<td class="calories">280</td>
</tr>
<tr class="foods foods_s-fndds2-f41210200">
<td class="name">
<span class="truncated"><a href="Javascript://">kathmandu…</a></span>
<span class="complete"><a href="Javascript://">USA(New</a><br>
York)</span>
</td>
</tr>
<tr class="folder foods foods_undefined">
<td class="name" colspan="4"><span></span>Black, brown, or Bayo beans, dry, cooked</td>
<td class="actions"><a class="open" href="Javascript://"></a></td>
</tr>
<tr class="foods foods_s-fndds2-f41210200">
<td class="name">
<span class="truncated"><a href="Javascript://">kathmandu…</a></span>
<span class="complete"><a href="Javascript://">India(New</a><br>
Delhi)</span>
</td>
</tr>
</table>
我尝试了这个查询,但只提供了
Black bean sauce
Black bean chicken
我试过的代码:
List<WebElement> folder = driver.findElements(By.xpath("//tr/td[@class='name']/a"));
for (WebElement webElement : folder) {
String foodName = webElement.getText();
System.out.println("text======" + foodName);
}
答案 0 :(得分:2)
使用管道|
选择更多节点:
//tr/td[@class='name']/a | //tr/td[@class='name']/span[@class='complete']