【问题标题】:HTML xpath selecting preceding sibling of parent elementHTML xpath 选择父元素的前一个兄弟
【发布时间】:2017-01-25 15:05:47
【问题描述】:

以下是我正在开发的应用程序的 html:

<td class="ui-selection-column" ng-reflect-ng-class="[object Object]" ng-reflect-ng-style="[object Object]" style="width: 38px; display: table-cell;">

<td ng-reflect-ng-class="[object Object]" style="display: table-cell;">
    <span class="ui-column-title">NAME</span>
    <span class="ui-cell-data">Name1</span>
</td>

我可以使用 text()=Name1 编写 xpath 来选择 td 元素内的 span。

//td[@style='display: table-cell;']/span[text()='Name1']

现在,我如何在上下文中选择上面的第一个 td 元素和较低的 td 元素。我在 Xpath 下面写了选择 td 元素的前一个兄弟,但没有工作。

//td[@style='display: table-cell;']/span[text()='VendorContact_Name1']/preceding-sibling::td[@class='ui-selection-column']

我有什么遗漏吗?有什么建议么?

【问题讨论】:

    标签: javascript html css xpath selenium-webdriver


    【解决方案1】:

    这是根据你的第一个 xpath:

    //td[@style='display: table-cell;']/span[text()='Name1']/../preceding-sibling::td[@class='ui-selection-column'][1]
    

    因为在您的 xpath 中您选择的是 span 节点,而不是 td,如果您想选择包含 spantd,它会类似于:

    //td[@style='display: table-cell;' and span[text()='Name1']]/preceding-sibling::td[@class='ui-selection-column'][1]
    

    【讨论】:

    • 太棒了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多