首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么webdriver为不同的id和xPath索引返回相同的元素?

为什么webdriver为不同的id和xPath索引返回相同的元素?
EN

Stack Overflow用户
提问于 2018-01-31 12:22:45
回答 3查看 81关注 0票数 0

我在显色剂中遇到了一个问题:web元素列表只返回第一项(也试图用壁虎司机完成任务,但结果是一样的)。

森查创建的网格结构是:

代码语言:javascript
复制
div-containers
    table (as row)
        tr (only one)
            td (as column/cell)
            td (as column/cell)
            ...
    table (as row)
    table (as row)

以下是HTML代码:

代码语言:javascript
复制
<div id="grid-1612-body" data-ref="body" class="x-panel-body x-grid-no-row-lines x-grid-body x-panel-body-default x-panel-body-default x-noborder-rl" role="presentation" style="width: 1069px; left: 0px; height: 585px; top: 29px;">
    <div class="x-grid-view x-fit-item x-grid-view-default x-unselectable x-scroller" role="rowgroup" id="gridview-1625" tabindex="0" data-componentid="gridview-1625" style="overflow-x: hidden; overflow-y: auto; margin: 0px; width: 1069px; height: 583px;">
        <div class="x-tab-guard x-tab-guard-after" tabindex="-1" data-tabindex-value="0" data-tabindex-counter="1"></div>
        <div class="x-grid-item-container" role="presentation" style="width: 1052px;">
            <table id="gridview-1625-record-218" role="presentation" data-boundview="gridview-1625" data-recordid="218" data-recordindex="0" class="x-grid-item" cellpadding="0" cellspacing="0" style=";width:0">
                <tbody>
                    <tr class="  x-grid-row" role="row">
                        <td class="x-grid-cell x-grid-td x-grid-cell-templatecolumn-1614 x-grid-cell-first x-unselectable" style="width:40px;" role="gridcell" tabindex="-1" data-columnid="templatecolumn-1614">
                            <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">
                                <div>&nbsp;</div>
                                <div>
                                    <img src="resources/pir-core/images/icons/star.gray.svg" title="Соглашение создано">&nbsp;</div>
                                </div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-debtAmount x-unselectable" style="width:130px;" role="gridcell" tabindex="-1" data-columnid="debtAmount">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:right;">
                                    <b>9,36 $</b>
                                </div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1616 x-unselectable" style="width:80px;" role="gridcell" tabindex="-1" data-columnid="gridcolumn-1616">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">66 month</div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-debtAmountsByActionTypes x-unselectable" style="width:40px;" role="gridcell" tabindex="-1" data-columnid="debtAmountsByActionTypes">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">
                                    <div>&nbsp;</div>
                                    <div>&nbsp;</div>
                                </div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-debtorName x-wrap-cell x-unselectable" style="width: 135px;" role="gridcell" tabindex="-1" data-columnid="debtorName" id="ext-element-43">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;" id="ext-element-42">Sherlock Holmes</div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1619  x-wrap-cell x-unselectable" style="width: 135px;" role="gridcell" tabindex="-1" data-columnid="gridcolumn-1619">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">Baker Street 221B</div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-ownersNumber x-unselectable" style="width:80px;" role="gridcell" tabindex="-1" data-columnid="ownersNumber" id="ext-element-39">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">1</div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-lastPay x-unselectable" style="width:165px;" role="gridcell" tabindex="-1" data-columnid="lastPay">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:right;">
                                    <table width="100%">
                                        <tbody>
                                            <tr>
                                                <td style="text-align: right; font-weight: bold;">1 532,02 $</td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td style="text-align: right; color: silver;">117 days</td>
                                                <td></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-templatecolumn-1622 x-unselectable" style="width: 67px;" role="gridcell" tabindex="-1" data-columnid="templatecolumn-1622">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">
                                    <table width="100%">
                                        <tbody>
                                            <tr>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td style="color: silver;">2596949  </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-callStatus x-pir-cursor-pointer x-unselectable" style="width:90px;" role="gridcell" tabindex="-1" data-columnid="callStatus">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">
                                    <div class="call-status ">
                                        <div>
                                            <i class="call-status-icon"></i>0
                                        </div>
                                        <div style="color: silver;">&nbsp;</div>
                                    </div>
                                </div>
                            </td>
                            <td class="x-grid-cell x-grid-td x-grid-cell-mailStatus x-pir-cursor-pointer x-grid-cell-last x-unselectable" style="width:90px;" role="gridcell" tabindex="-1" data-columnid="mailStatus">
                                <div unselectable="on" class="x-grid-cell-inner " style="text-align:;">
                                    <div class="post-status ">
                                        <div>
                                            <i class="post-status-icon"></i>0
                                        </div>
                                        <div>&nbsp;</div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table id="gridview-1625-record-219" role="presentation" data-boundview="gridview-1625" data-recordid="219" data-recordindex="1" class="x-grid-item x-grid-item-alt" cellpadding="0" cellspacing="0" style=";width:0">
                    ...
                </table>
                <table id="gridview-1625-record-220" role="presentation" data-boundview="gridview-1625" data-recordid="220" data-recordindex="2" class="x-grid-item" cellpadding="0" cellspacing="0" style=";width:0">
                    ...
                </table>
                <table id="gridview-1625-record-221" role="presentation" data-boundview="gridview-1625" data-recordid="220" data-recordindex="2" class="x-grid-item" cellpadding="0" cellspacing="0" style=";width:0">
                    ...
                </table>
                <table id="gridview-1625-record-222" role="presentation" data-boundview="gridview-1625" data-recordid="220" data-recordindex="2" class="x-grid-item" cellpadding="0" cellspacing="0" style=";width:0">
                    ...
                </table>
                ...
                <div id="loadmask-1660-msgTextEl" data-ref="msgTextEl" class="x-mask-msg-text" role="presentation">Loading...</div>
            </div>
        </div>
    </div>
</div>

不管我如何尝试获得第二个元素(或者任何不是-优先的元素),都只返回第一个元素:

代码语言:javascript
复制
// getting n-th item of WebElements list
List<WebElement> debtorElements = driver.findElements(By.xpath("//div[@id='grid-1612-body']/div/div[@class='x-grid-item-container']/table"));
debtorElements.get(2).findElement(By.xpath("//td[@data-columnid='debtorName']/div")).getText()

// by xPath with index
driver.findElement(By.xpath("//div[@id='grid-1612-body']/div/div[@class='x-grid-item-container']/table[2]")).findElement(By.xpath("//table/tbody/tr[1]/td[5]/div")).getText()
// by xPath with id
driver.findElement(By.xpath("//table[@id='gridview-1625-record-219']")).findElement(By.xpath("//table/tbody/tr[1]/td[5]/div")).getText()

Chrome浏览器:版本63.0.3239.84 (官方版本),(64位)。

有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-01-31 12:41:50

在使用//时,您是在告诉WebDriver从根节点(<html>)中查找。使用.告诉它从位置看

代码语言:javascript
复制
debtorElements.get(2).findElement(By.xpath(".//td[@data-columnid='debtorName']/div")).getText()
                                            ^ add this
票数 2
EN

Stack Overflow用户

发布于 2018-01-31 12:35:26

通常,当我们在Chrome中使用“复制Xpath”复制Xpath时,它并不是完全复制的。我的意思是它完全给出了Xpath,但没有给出元素的索引。我们得用手工索引。如果它起作用的话,试试这个:

代码语言:javascript
复制
// by xPath with index
  driver.findElement(By.xpath("//div[@id='grid-1612-body']/div/div[@class='x-grid-item-container']/table[2]")).findElement(By.xpath("//table/tbody/tr[1]/td[5]/div[your webelement index]")).getText()

//Same will be the case with Id

我也希望你知道元素的迭代使用任何你想要的循环,所以也试一试。

票数 0
EN

Stack Overflow用户

发布于 2018-01-31 12:39:14

问题可能在于你正在尝试:

代码语言:javascript
复制
debtorElements.get(2).findElement(By.xpath("//td[@data-
columnid='debtorName']/div")).getText()

这可能会得到第一个元素的文本。你可以这样做:

代码语言:javascript
复制
foreach(el in debtorElements)
{
    el.getText();
}

这样,您将尝试获取列表中每个元素的文本。我用C#编程,所以请原谅任何语法错误。

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

https://stackoverflow.com/questions/48542103

复制
相关文章

相似问题

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