我有以下 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/