java - Selenium:选择间接兄弟元素

标签 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']")  

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

最佳答案

使用这个:-

//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']

关于java - Selenium:选择间接兄弟元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36580651/

相关文章:

java - 如何使用列表实现深度优先遍历测试?

java - Spring 调试记录在单独的日志文件中

java - 如何在android项目中包含selenium库?

java - 如何使用java将phantomjs连接到selenium

python - 如何使用 Selenium 和 Python 在 https ://e. mail.ru/login 中发送密码元素内的文本

sql-server - select * 只返回一个值

java - 如何在 Java 8 中直接使用函数作为函数类型

java - 从共享路径打开文件时获取 IOException

php - mySQL 从多个外键返回字段名称

mysql - 如何用 'table a' .'column b' 替换 'table b' .'column b' 如果 'table a' .'column a' 匹配 'table b' .'column a'