繁体   English   中英

硒:选择间接同级元素

[英]Selenium: Select indirect sibling element

我有以下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']")  

不起作用。
需要您的帮助来选择它

用这个:-

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

要么

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

ID也被提到a标签。 因此,您也可以使用id:

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

要么

//a[@id='js_c']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM