本文介绍了如何获得selenium xpath中的许多div之间的子父级网络元素组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定文本post2的情况下,给定文章的文本,我想得到子父元素< div class =post/> 这是代码
中的第二个div,在页面中有很多帖子。



我尝试使用这样的xpath,我确实认为这是可能的找到子元素父元素之间的元素:
// div [@ id ='content'] // span [contains(text(),'post2')] /.//* [@ class = 'post']

 < div id =content> 
< div class =post>
< div class =right>
< div class =content>
< div class =post-content>
< span>
post1
< / span>
< / div>
< / div>
< / div>
< / div>
< div class =post>
< div class =right>
< div class =content>
< div class =post-content>
< span>
post2
< / span>
< / div>
< / div>
< / div>
< / div>
< / div>

是否存在其他解决方案或者xpath错误?
span 检查'作为的谓词

< div class =post> :

  // div [@ id ='内容'] / div [@ class ='post'] [.// span [contains(text(),'post2')]] 

通过这种方式,您不需要将树返回到祖先< div class =post> 之后直接进入 span


I want to get the sub parent element <div class="post"/> given the text of a post in this case given the text "post2" that is the second div in the codethere are many posts in a page.

I tried to use an xpath like this that I did thinking that is possible find between in the sub-parents web elements:"//div[@id='content']//span[contains(text(), 'post2')]/.//*[@class='post']"

<div id="content">
    <div class="post">
        <div class="right">
            <div class="content">
                <div class="post-content">
                    <span>
                        post1
                    </span>
                </div>
            </div>
        </div>  
    </div>
    <div class="post">
        <div class="right">
            <div class="content">
                <div class="post-content">
                    <span>
                        post2
                    </span>
                </div>
            </div>
        </div>  
    </div>
</div>

exists any other solution for that or maybe the xpath is wrong?

解决方案

Alternatively, you can put the 'span checking' as predicate for the <div class="post"> :

//div[@id='content']/div[@class='post'][.//span[contains(text(),'post2')]]

This way you don't need to go back up the tree to the ancestor <div class="post"> after going straight to the span

这篇关于如何获得selenium xpath中的许多div之间的子父级网络元素组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 04:16