当页面调整大小时,我有一个链接隐藏了元素,所以即时通过前一个元素尝试获取该元素。使用Xpath获取前面的兄弟姐妹
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<i class="icon icon-doc-new"></i>
<span class="hidden-sm dropdown-label">Create</span>
<span class="caret hidden-sm"></span>
</a>
获得 “正常” 的元素我使用XPath:
.//*[@id='main-menu']/li/a/span[contains(text(), 'Create')]
的话,我想获得元素<i class="icon icon-doc-new"></i>
像这样
.//*[@id='main-menu']/li/a/span[contains(text(), 'Create')]/precending-sibling::i[0] //(also tried different indexes]
试过一对夫妇其他方法,但没有去。
任何提示?
这工作马上。你介意多解释一下为什么? – Dymond
其实,我刚刚注意到你的'XPath'表达式中有一个输入错误:'前同胞'应该是'前同胞'。所以你的'XPath'也应该是正确的。我的意思是用'[1]'索引,而不是'[0]' – Andersson