首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium WebDriver XPath指向第二行,但findElements返回第一行

Selenium WebDriver XPath指向第二行,但findElements返回第一行
EN

Stack Overflow用户
提问于 2018-06-21 19:35:28
回答 1查看 222关注 0票数 0

我的HTML如下所示:

代码语言:javascript
复制
<table id="data-table" class="table table-striped table-bordered table-hover dataTable no-footer" role="grid" style="width: 100%;">
    <thead>
        <tr role="row" style="height: 0px;"><th scope="col" class="sorting" aria-controls="data-table" rowspan="1" colspan="1" style="width: 277px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">Store Number</div></th><th scope="col" class="sorting" aria-controls="data-table" rowspan="1" colspan="1" style="width: 241px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">Store Name</div></th><th scope="col" class="sorting" aria-controls="data-table" rowspan="1" colspan="1" style="width: 156px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">Active</div></th><th scope="col" class="sorting" aria-controls="data-table" rowspan="1" colspan="1" style="width: 237px; padding-top: 0px; padding-bottom: 0px; border-top-width: 0px; border-bottom-width: 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">Action</div></th></tr>
    </thead>

    <tbody>







    <tr id="store-3" data-model="{&quot;Id&quot;:3,&quot;Name&quot;:&quot;a&quot;,&quot;IsActive&quot;:true,&quot;DBVersion&quot;:&quot;&quot;,&quot;Address&quot;:{&quot;Address&quot;:&quot;&quot;,&quot;Address2&quot;:&quot;&quot;,&quot;State&quot;:&quot;&quot;,&quot;City&quot;:&quot;&quot;,&quot;Zip&quot;:0},&quot;Phone&quot;:&quot;&quot;}" role="row" class="odd">
            <th scope="row" class="sorting_1">3</th>
            <td>a</td>
            <td>
                <label class="tgl">
                    <input onclick="ToggleStoreActive(this,event)" class="form-isactive" type="checkbox" checked="">
                    <span class="tgl_body">
                        <span class="tgl_switch"></span>
                        <span class="tgl_track">
                            <span class="tgl_bgd"></span>
                            <span class="tgl_bgd tgl_bgd-negative"></span>
                        </span>
                    </span>
                </label>
            </td>
            <td>
                <button class="btn btn-danger" onclick="DeleteStore(this)">
                    <i class="fa fa-trash-o" aria-hidden="true"></i>
                </button>
                <button class="btn btn-primary" onclick="PopModel(this)">
                    <i class="fa fa-pencil" aria-hidden="true"></i>
                </button>
            </td>
        </tr><tr id="store-4" data-model="{&quot;Id&quot;:4,&quot;Name&quot;:&quot;b&quot;,&quot;IsActive&quot;:true,&quot;DBVersion&quot;:&quot;&quot;,&quot;Address&quot;:{&quot;Address&quot;:&quot;&quot;,&quot;Address2&quot;:&quot;&quot;,&quot;State&quot;:&quot;&quot;,&quot;City&quot;:&quot;&quot;,&quot;Zip&quot;:0},&quot;Phone&quot;:&quot;&quot;}" role="row" class="even">
            <th scope="row" class="sorting_1">4</th>
            <td>b</td>
            <td>
                <label class="tgl">
                    <input onclick="ToggleStoreActive(this,event)" class="form-isactive" type="checkbox" checked="">
                    <span class="tgl_body">
                        <span class="tgl_switch"></span>
                        <span class="tgl_track">
                            <span class="tgl_bgd"></span>
                            <span class="tgl_bgd tgl_bgd-negative"></span>
                        </span>
                    </span>
                </label>
            </td>
            <td>
                <button class="btn btn-danger" onclick="DeleteStore(this)">
                    <i class="fa fa-trash-o" aria-hidden="true"></i>
                </button>
                <button class="btn btn-primary" onclick="PopModel(this)">
                    <i class="fa fa-pencil" aria-hidden="true"></i>
                </button>
            </td>
        </tr><tr id="store-5" data-model="{&quot;Id&quot;:5,&quot;Name&quot;:&quot;c&quot;,&quot;IsActive&quot;:true,&quot;DBVersion&quot;:&quot;&quot;,&quot;Address&quot;:{&quot;Address&quot;:&quot;&quot;,&quot;Address2&quot;:&quot;&quot;,&quot;State&quot;:&quot;&quot;,&quot;City&quot;:&quot;&quot;,&quot;Zip&quot;:0},&quot;Phone&quot;:&quot;&quot;}" role="row" class="odd">
            <th scope="row" class="sorting_1">5</th>
            <td>c</td>
            <td>
                <label class="tgl">
                    <input onclick="ToggleStoreActive(this,event)" class="form-isactive" type="checkbox" checked="">
                    <span class="tgl_body">
                        <span class="tgl_switch"></span>
                        <span class="tgl_track">
                            <span class="tgl_bgd"></span>
                            <span class="tgl_bgd tgl_bgd-negative"></span>
                        </span>
                    </span>
                </label>
            </td>
            <td>
                <button class="btn btn-danger" onclick="DeleteStore(this)">
                    <i class="fa fa-trash-o" aria-hidden="true"></i>
                </button>
                <button class="btn btn-primary" onclick="PopModel(this)">
                    <i class="fa fa-pencil" aria-hidden="true"></i>
                </button>
            </td>
        </tr></tbody>
</table>

并尝试使用以下Groovy代码获取第二行(具有"Store Number“4和"Store Name”'b')的行:

代码语言:javascript
复制
'get the initial number of data rows'
String dataRowsSelector = '//table[contains(@id, "data-table")]/tbody/tr'

List<WebElement> dataRows = driver.findElements(By.xpath(dataRowsSelector))

int initialRowCount = dataRows.size()

assertNotEquals(index, 0)

'find the row pointed to by the index'
String rowSelector = String.format('//table[contains(@id, "data-table")]/tbody/tr[%d]', (index + 1))

assertEquals(rowSelector, '//table[contains(@id, "data-table")]/tbody/tr[2]')

WebElement rowToDelete = driver.findElement(By.xpath(rowSelector))

'get the delete button'
String deleteButtonSelector = '//button[./i[contains(concat(" ", @class, " "), " fa-trash-o ")]]'

WebElement deleteButton = rowToDelete.findElement(By.xpath(deleteButtonSelector))

println(rowToDelete.findElement(By.xpath('//td[1]')).getAttribute('innerText'))

assertTrue(rowToDelete.findElement(By.xpath('//td[1]')).getAttribute('innerText').contains('b'))

我运行这段代码,rowToDelete成为第一个数据行,而不是第二个数据行(我再次尝试使用3作为索引,它仍然是第一行),尽管针对目标xpath的断言有效。另外,该WebElement的第一个WebElement包含a而不是b。(当我尝试使用3作为索引时,我再次获得了a,尽管我期待着c

我该怎么纠正这种奇怪的行为?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-21 19:41:39

显然,问题在于我的XPath本身。当尝试选择相对于其他WebElementWebElement,使用XPath时,我应该在//前面放置一个点,以给出范围。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50976306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档