【问题标题】:Work with XPath to select the item with the highest z-index value使用 XPath 选择具有最高 z-index 值的项目
【发布时间】:2018-11-05 19:01:57
【问题描述】:

需要使用具有最高 z-index 值的选择器

<div style="position: fixed;width: 400px;height: 278px;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 6px;bottom: 0px;transition: transform 2s ease 0s;right: 120px;z-index: 2;" class="Cl"><iframe id="t1dxvk58p7cu" name="t1dxvk58p7cu" class="Xyqxtc" allow="camera" style="height: 100%; width: 100%; background: transparent; overflow: hidden; border: none;"></iframe></div>
<div style="position: fixed;width: 400px;height: 278px;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 6px;bottom: 0px;transition: transform 2s ease 0s;right: 120px;z-index: 3;" class="Cl"><iframe id="t1dxvk58p7cu" name="t1dxvk58p7cu" class="Xyqxtc" allow="camera" style="height: 100%; width: 100%; background: transparent; overflow: hidden; border: none;"></iframe></div>
<div style="position: fixed;width: 400px;height: 278px;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 6px;bottom: 0px;transition: transform 2s ease 0s;right: 120px;z-index: 4;" class="Cl"><iframe id="t1dxvk58p7cu" name="t1dxvk58p7cu" class="Xyqxtc" allow="camera" style="height: 100%; width: 100%; background: transparent; overflow: hidden; border: none;"></iframe></div>

我在下面尝试但失败了

//div[@class='Cl'][substring-after(@style,'z-index:') > substring-after(../div[@class='Cl']/@style,'z-index:')]

【问题讨论】:

    标签: html xpath


    【解决方案1】:

    这是一个使用正则表达式 replacemax 的 XPath 2.0 解决方案

    //div[@class='Cl' and replace(@style,'^.*?z-index: (\d+).*?$','$1') = max(../div[@class='Cl']/replace(@style,'^.*?z-index: (\d+).*?$','$1'))]
    

    主要思想是

    • 使用替换从样式属性中提取 z-index
    • 并将其与相似元素的最大 z-index 进行比较:div[id = max(../div/id)]
      (id 等于提取的 z-index)

    Live Demo

    这是另一个遵循架构div/[not(..div/id &gt; id)]

    //div[@class='Cl' and not(../div[@class='Cl']/replace(@style,'^.*?z-index: (\d+).*?$','$1') > replace(@style,'^.*?z-index: (\d+).*?$','$1'))]
    

    参考资料:

    【讨论】:

    • 你可以重做它以在 chrome 中工作,但我不明白该怎么做。
    • @GamiDPC 不,因为 XPath 1.0 不支持正则表达式,而且我看不到在不提取 z-index 的情况下重写它的方法。最好的办法是使用编程语言(如 JS、Python 等)来处理所有选定 div 比较时的提取。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2020-07-30
    相关资源
    最近更新 更多