【发布时间】:2023-03-14 08:50:02
【问题描述】:
我的数据行很少,它们没有可跟踪的 id 或类,因此需要一个 child/followed 类型的 XPath。 以下是 HTML 内容:
<tr class="v-formlayout-row v-formlayout-firstrow" xpath="1">
<td class="v-formlayout-captioncell">
<div class="v-caption v-caption-smalllabel v-caption-hide-indicator v-caption-hasdescription"><span id="gwt-uid-6138" for="gwt-uid-6139">Unit type</span></div>
</td>
<td class="v-formlayout-errorcell">
<div class="v-formlayout-error-indicator"></div>
</td>
<td class="v-formlayout-contentcell">
<div class="v-horizontallayout v-layout v-horizontal v-widget smalllabel v-horizontallayout-smalllabel hide-indicator v-horizontallayout-hide-indicator" id="gwt-uid-6139" aria-labelledby="gwt-uid-6138">
<div class="v-slot v-slot-hide-indicator">
<div class="v-formlayout v-layout v-widget hide-indicator v-formlayout-hide-indicator">
<table cellpadding="0" cellspacing="0" role="presentation">
<colgroup>
<col>
</colgroup>
<tbody>
<tr class="v-formlayout-row v-formlayout-firstrow v-formlayout-lastrow">
<td class="v-formlayout-captioncell">
<div class="v-caption v-caption-tiny v-caption-smalllabel"></div>
</td>
<td class="v-formlayout-errorcell">
<div class="v-formlayout-error-indicator"></div>
</td>
<td class="v-formlayout-contentcell">
<div class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w" style="">CHDB </div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="v-slot v-slot-hide-indicator">
<div class="v-formlayout v-layout v-widget hide-indicator v-formlayout-hide-indicator">
<table cellpadding="0" cellspacing="0" role="presentation">
<colgroup>
<col>
</colgroup>
<tbody>
<tr class="v-formlayout-row v-formlayout-firstrow v-formlayout-lastrow">
<td class="v-formlayout-captioncell">
<div class="v-caption v-caption-tiny v-caption-smalllabel"></div>
</td>
<td class="v-formlayout-errorcell">
<div class="v-formlayout-error-indicator"></div>
</td>
<td class="v-formlayout-contentcell">
<div class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w">F1080 </div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
这里Unit type是需要作为父元素的,这个值不会改变,但是后面的元素CHDB和F1080 变化,我们需要验证这两个元素。
为此,我需要一个 XPath,它将 Unit 类型作为父元素以及我们作为子元素获得的值,并且对于同一模式中的多个值需要这个,所以这个会很有帮助。
目前使用
(//tr//child::td[contains(@class,'v-formlayout-contentcell')]//child::div[contains(@id,'gwt-uid')])[1]
(//tr//child::td[contains(@class,'v-formlayout-contentcell')]//child::div[contains(@id,'gwt-uid')])[2]
这不是一个好的做法,因此将第一个值作为父值,将下一个值作为子值或使用兄弟函数需要可重用的 XPath
【问题讨论】:
-
您是否要提取文本,例如
CHDB和F1080?
标签: java selenium xpath webdriverwait xpath-1.0