在没有迭代的情况下提取表中特定行的父代
考虑下表结构包含许多具有多个列值的行。我需要识别特定行的父级,这些行必须使用单元格进行标识。在没有迭代的情况下提取表中特定行的父代
<table class = 'grid'>
<thead id = 'header'>
</thead>
<tbody>
<tr>
<td>
<span class="group">
<span class="group__link"><a class="disabledlink"">copy</a>
</span>
</span>
</td>
<td class="COLUMNNAME">ACE</td>
<td class="COLUMNLONGNAME">Adverse Childhood Experiences</td>
<li>Family Medicine</li>
<li>General Practice</li>
</td>
<td class="COLUMNSEXFILTER">Both</td>
<td class="COLUMNAGEFILTERMIN">Any</td>
<td class="COLUMNTYPE">Score Only</td>
</tr>
<tr>
<td class="nowrap" showactionitem="2">
<span class="group">
<span class="group__link"><a onclick="Check()" href="#">copy</a>
</span>
</span>
</td>
<td class="COLUMNNAME">AM-PAC</td>
<td class="COLUMNLONGNAME">AM-PAC Generic Outpatient Basic Mobility Short Form</td>
<td class="COLUMNNOTE"></td>
<td class="COLUMNRESTRICTEDYN">No</td>
<td class="COLUMNSPECIALTYID"></td>
<td class="COLUMNSEXFILTER">Both</td>
<td class="COLUMNAGEFILTERMIN">Any</td>
<td class="COLUMNTYPE">Score Only</td>
</tr>
<tr></tr>
<tr></tr>
</tbody></thead>
</table>
同样,该表包含大约100行。我使用迭代做了同样的工作,并且工作正常。 是否有可能找到没有迭代的特定行的父代?
可以使用此之一:
父::节点()
的例子将选择编号=“电子邮件”的输入标签的父节点的下方。 例://输入[@ ID = '电子邮件'] /父:: *
以上也可以重写为
//输入[@ ID = '电子邮件'] /。 。
没有必要对任何幻想,的Watir有Element#parent
方法。
您可以使用parent
方法查找元素的父项。假设你已经找到了一个表格单元格,我们称之为cell
,你可以使用parent
,然后用另一个呼叫该行的母公司其行parent
:
cell.parent
#=> a <tr> element
cell.parent.parent
#=> the parent of the specific row - a <tbody> element in this case
连锁多种parent
调用可以变得繁琐和困难保持。例如,您必须拨打parent
4次才能获取“复制”链接的表格单元格。如果你是一个祖先(即不是直接母公司)后,你最好使用XPath:
cell.table(xpath: './ancestor::table')
#=> the <table> element containing the cell
browser.link(text: 'copy').tr(xpath: './ancestor::tr')
#=> the <tr> element containing a copy link
希望Issue 451不久将付诸实施,这将消除对XPath的需要。您可以拨打:
cell.parent(tag_name: 'table') # equivalent to `cell.table(xpath: './ancestor::table')`
这现在在Watir 6.2中实现! http://watir.github.io/watir-6-2/没有更多的XPath! – titusfortner
您是否想要使用单元格查找行? – Gopal
您是否可以添加您的编码以供参考? – Gopal
当你说“特定行的父项”时,你是否试图获得包含单元格的tr'元素或包含该单元格的'tr'的父项?措辞表明后者,但我认为你想要前者? –