XPath 选择以下兄弟姐妹 [英] XPath to select following-sibling

查看:34
本文介绍了XPath 选择以下兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前拥有的代码:

<ul class="nav nav-pills nav-stacked"><li><li><li><li><li><部分><span name="merchant">ABZ</span></节><部分><span class="glyphicon glyphicon-pencil" name="edit"></span><span class="glyphicon glyphicon-remove" name="delete"></span></节><li><li><li><li><div class="add-item bottom" name="new-merchant">

我尝试了以下方法:

xpath=//span[contains(.,'ABZ')]/following-sibling::section/span[@name='edit']xpath=//span[contains(.,'ABZ')]/following-sibling::span[1]

我正在使用 selenium,我希望它点击 ABZ span 之后的编辑按钮.

解决方案

这个 xpath 在 Chrome 中为我工作,使用你的 html:

//span[contains(text(), 'ABZ')]/../following-sibling::section/span[@name='edit']

编辑

.. 表示上升一个级别... 将选择器从 span 带到 section.然后 following-sibling 在匹配的 span 的父 section 之后找到 section.

您的原始选择器匹配 span,然后寻找 following-siblingspan.

This is the code that I currently have:

<div>
<ul class="nav nav-pills nav-stacked">
<li>
<li>
<li>
<li>
<li>
<section>
<span name="merchant">ABZ</span>
</section>
<section>
<span class="glyphicon glyphicon-pencil" name="edit"></span>
<span class="glyphicon glyphicon-remove" name="delete"></span>
</section>
</li>
<li>
<li>
<li>
<li>
</ul>
<div class="add-item bottom" name="new-merchant">
</div>

I have tried the following:

xpath=//span[contains(.,'ABZ')]/following-sibling::section/span[@name='edit']
xpath=//span[contains(.,'ABZ')]/following-sibling::span[1]

I am using selenium, and I want it to click on the edit button that is right after the ABZ span.

解决方案

This xpath worked for me in Chrome, using your html:

//span[contains(text(), 'ABZ')]/../following-sibling::section/span[@name='edit']

Edit

The .. means to go up a level. The .. takes the selector from span to section. Then the following-sibling finds the section after the parent section of your matching span.

Your original selector was matching span, then looking for a following-sibling to span.

这篇关于XPath 选择以下兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆