Selenium:选择间接兄弟元素

时间:2016-04-12 17:53:40

标签: java selenium select selenium-webdriver siblings

我有以下HTML结构:

<div class="UFICommentContentBlock">
    <div class="UFICommentContent">
    <span>
    <span>
        <span data-ft="{"tn":"K"}">
            <span class="UFICommentBody">
                <span>My comment text</span>
            </span>
        </span>
    </span>
    <div class="UFITranslatedText"></div>
    <span></span>
</div>
<div class="fsm fwn fcg UFICommentActions">
    <a class="UFILikeLink" data-ft="{"tn":">"}" data-testid="ufi_comment_like_link" href="#" role="button" title="Like this comment">Like</a>
    <span role="presentation" aria-hidden="true"> · </span>
    <a class="UFIReplyLink" href="#" role="button">Reply</a>
    <span role="presentation" aria-hidden="true"> · </span>
    <span>
</div>
<a class="UFICommentCloseButton _5upq _5upr _5upp _42ft" data-testid="ufi_comment_close_button" data-hover="tooltip" data-tooltip-alignh="center" data-tooltip-content="Edit or delete this" href="#" id="js_c">   </a>
</div>  

这是Facebook评论区。 我在帖子下面有几条评论,每条都有相同的结构。 我可以通过

找到所需的评论
xpath("//div[@class='UFICommentContentBlock']//span[@class='UFICommentBody']//span[text()='My comment text']")  

我需要访问此评论的编辑评论按钮,该评论也是UFICommentContentBlock的孩子,但不是包含评论文字的元素的直接兄弟,所以

xpath("//div[@class='UFICommentContentBlock']//span[@class='UFICommentBody']//span[text()='.']/following-sibling::div[@class='fsm fwn fcg UFICommentActions']/a[@class='UFICommentCloseButton _5upq _5upr _5upp _42ft']")  

不起作用。
需要您的协助才能选择它

1 个答案:

答案 0 :(得分:1)

使用: -

//span[text()='My comment text']/ancestor::div[@class='UFICommentContentBlock']//a[contains(@class,'UFICommentCloseButton')]

OR

//span[text()=.]/ancestor::div[@class='UFICommentContentBlock']//a[contains(@class,'UFICommentCloseButton')]
ID标记也提到了

a。所以你也可以使用id: -

//span[text()=.]/ancestor::div[@class='UFICommentContentBlock']//a[@id='js_c']

OR

//a[@id='js_c']