当前位置: 首页 > 知识库问答 >
问题:

Selenium-尝试使用“contains”通过表单元格的内容定位表单元格

海岳
2023-03-14

这是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]”);

但是,我想通过使用“包含”访问单元格的内容。

我尝试了以下2种方法:

驾驶员findElement(By.xpath(“//table[@id='listMainTable']//tr[contains(td[1],'TestReport')]]/td[2]”);

驾驶员findElement(By.xpath(“//table[@id='listMainTable']/tbody/tr/div[contains(text(),'TestReport')]”);

两者都抛出由:org引起的错误。openqa。硒。NoTouchElementException:无法定位元素

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

共有2个答案

章茂
2023-03-14

你的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>
谷梁博易
2023-03-14

你可以试试这样的

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

谢谢你,穆拉里

 类似资料:
  • 这是html代码: driver.findelement(by.xpath(“//table[@id='list maintable']//tr[contains(TD[1],'TestReport')]/td[2]”)); driver.findelement(by.xpath(“//table[@id='list maintable']/tbody/tr/div[contains(text()

  • 问题内容: 我有一个由用户创建的单元格调用的表视图。每个单元都有文本视图。我想通过“文本视图”的内容更改单元格的高度。这是我尝试的: 问题答案: 绑定您与来自四面八方的使用边际约束。( 前置,顶部和底部的约束 ) 禁用textView滚动 在 viewDidLoad()中 添加以下内容。 这将使您的单元格大小根据您的textview内容大小。 看一下结果: 您无需编写 heightForRowAt

  • 我正在用Selenium IDE测试一个包含表的网页。我想通过CSS选择器验证表格单元格(行,列)中的文本。表的HTML结构非常简单。下面是一个表的示例,其中每一行表示一个人的不同属性: 在Selenium IDE中,我能够使用以下CSS选择器在包含AGE的表行中找到“49”: 当我将这些方法导出为JUnit代码时,问题就来了。以下JUnit代码块工作: (使用方法1) WebDriver.Fin

  • 本文向大家介绍JS获取表格内指定单元格html内容的方法,包括了JS获取表格内指定单元格html内容的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JS获取表格内指定单元格html内容的方法。分享给大家供大家参考。具体如下: 下面的代码先通过表格对象的rows获得指定的行的所有单元格数组,然后定位指定的单元格,通过单元格的innerHTML属性获得单元格的html内容 希望本文所述对

  • 问题内容: 我想在不打开应用程序的情况下对excel中的单元格进行调整。我想和詹金斯一起做,这样我就可以自动化(回归)测试。(这甚至可能吗?) 正如您在附上的图像中看到的那样,我努力将excel中的“ N”值更改为“ Y”值,反之亦然。我想通过Jenkins的用户界面来实现这一目标。我已经听说过类似参数化构建的内容,但是我无法在任何网站上获得帮助,也无法提供有关该主题的更多信息。 我想要实现的是这