我遇到了chromedriver的问题:网络元素列表只返回第一项(也尝试使用geckodriver完成任务,但结果相同)。< / p>
sencha创建的网格结构是:
div-containers
table (as row)
tr (only one)
td (as column/cell)
td (as column/cell)
...
table (as row)
table (as row)
这是HTML代码:
<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> </div>
<div>
<img src="resources/pir-core/images/icons/star.gray.svg" title="Соглашение создано"> </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> </div>
<div> </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;"> </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> </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>
无论我如何尝试获得第二个元素(或者不是第一个元素),只返回第一个元素:
// 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位)。
有什么建议吗?
答案 0 :(得分:2)
使用//
时,您告诉WebDriver
从根节点(<html>
)查找。使用.
告诉它从位置查看
debtorElements.get(2).findElement(By.xpath(".//td[@data-columnid='debtorName']/div")).getText()
^ add this
答案 1 :(得分:0)
通常当我们在Chrome中使用“复制Xpath”复制Xpath时,它不会被完全复制。我的意思是它完全给出了Xpath而不是元素的索引。我们必须使用手动索引。尝试这个,如果它的工作原理:
// 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
另外,我希望你知道使用你想要的任何循环来迭代元素,所以也要这样做 试试..
答案 2 :(得分:0)
问题可能在于您正在尝试:
debtorElements.get(2).findElement(By.xpath("//td[@data-
columnid='debtorName']/div")).getText()
这可能会获得第一个元素的文本。你可以这样做:
foreach(el in debtorElements)
{
el.getText();
}
这样您就可以尝试获取列表中每个元素的文本。我用C#编程,所以请原谅任何语法错误。