这是 html 代码:
<div id="listMain" class="listMain">
<table id="listMainTable" class="listTable" >
<thead>
<tbody id="mainTableBody">
<tr id="Node0" class="row" tabindex="0" >
<tr id="Node1" class="alternateRow" tabindex="-1" >
<tr id="Node2" class="row" tabindex="-1" >
<tr id="Node3" class="alternateRow" tabindex="-1" >
<tr id="Node4" class="row" tabindex="-1" >
<td class="cell" >
<td class="cell" >
<div id="detailView_listColumn_4" style="overflow: hidden" aria-describedby="detailView_mainTooltip">TestReport</div>
</td>
<td class="cell" >
<td class="cell" >
<tr id="Node5" class="row" tabindex="0" >
<tr id="Node6" class="alternateRow" tabindex="-1" >
</tbody>
</table>
</div>
我想访问第 5 行第 2 列的内容。
我可以通过直接访问给出行号和列号的单元格来做到这一点: driver.findElement(By.xpath("//table[@id='listMainTable']//tr[5]/td[ 2]"));
但是,我想使用“包含”通过其内容访问单元格。
我尝试了以下两种方法:
driver.findElement(By.xpath("//table[@id='listMainTable']//tr[contains(td[1], 'TestReport')]/td[2]"));
driver.findElement(By.xpath("//table[@id='listMainTable']/tbody/tr/div[contains(text(), 'TestReport')]"));
两者都会抛出错误 - 原因为:org.openqa.selenium.NoSuchElementException:无法找到元素。
我不知道这是否与表格单元格内的“div”中包含的“TestReport”有关。在这种情况下,我如何使用“包含”访问该特定单元格?
最佳答案
你可以尝试这样的事情
//div[contains(text(),'TestReport')]
谢谢你, 穆拉利
关于java - Selenium - 尝试使用 "contains"通过其内容来定位表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35414072/