【问题标题】:Selenium - Trying to locate table cell by its contents using "contains"Selenium - 尝试使用“包含”通过其内容定位表格单元格
【发布时间】:2016-05-26 15:22:55
【问题描述】:

这是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: Unable to locate element。

我不知道这是否与表格单元格内的“div”中包含的“TestReport”有关。在这种情况下,我如何使用“包含”访问该特定单元格?

【问题讨论】:

    标签: java selenium xpath webdriver contains


    【解决方案1】:

    你可以试试这样的

    //div[contains(text(),'TestReport')]
    

    谢谢你, 壁画

    【讨论】:

    • 我已经试过了 - driver.findElement(By.xpath("//table[@id='listMainTable']/tbody/tr/div[contains(text(), 'TestReport') ]"));这也给出了 NoSuchElementException。
    • 使用的和我指定的有所不同。请尝试一次。它工作正常 driver.findElement(By.xpath("//div[contains(text(),'TestReport')]"));谢谢
    • 成功了。谢谢。我有另一个问题。同一个表有许多行,在各自的“div”中具有相似的文本。例如,第一行有“Selenium_TestReport”,第二行有“Java_TestReport”,第五行有“TestReport”。使用 //div[contains(text(),'TestReport')],第一行被选中,因为它包含部分文本。如何使用 contains() 进行精确的字符串匹配,以便只选择第五行?
    • 你可以像这样在 xpath 中使用starts-with //div[contains(text(),'TestReport')][starts-with(text(),'Test')] 这将获取您只能使用 Test 的文本,所以没有 Selenium 没有 Java。如果您要查找的单元格包含 Selenium_TestReport,那么您可以在上面提到的开头使用 Selenium。我希望我会工作。谢谢
    【解决方案2】:

    您的 html 格式不正确。有几个标签没有相应的结束标签。

    您可以使用以下内容。

    driver.findElement(By.xpath("/html/div[@id='listMain']/table[@id='listMainTable']/tbody[@id='mainTableBody']/tr[5]/td[2]/div[contains(text(),'TestReport')]"));
    

    HTML

    <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>
        <tr id="Node5" class="row"      tabindex="0" />
        <tr id="Node6" class="alternateRow"     tabindex="-1" />
    
    </tbody>
    </table>
    </div>
    </html>
    

    【讨论】:

    • 如果我通过指定行号和列号(tr[5]/td[2])直接访问单元格,那么我不需要通过其内容找到单元格
    • driver.findElement(By.xpath("//div[contains(text(),'TestReport')]"));
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多