当前节点上方祖先的后代的 XPATH

问题描述 投票:0回答:1
<oc-resume-upload>
    <div data-test="resume-upload-container" class="form-section">
        <div class="flex flex-column">
            <h2 class="form-section-header form-section-header--required"> **Resume **</h2>
            <div>
                <div>
                    <div ocappdrag="" class="dropzone dropzone-upload">
                        <oc-file-upload-button _nghost-ng-c1329912542="">
                            <div _ngcontent-ng-c1329912542="">
                                **<button _ngcontent**-ng-c1329912542="" type="button" data-test="resume-upload" aria-label="Upload resume" class="padding--m button button--clean">
                                    <span class="font-size--s font--bold">
                                        <strong>Select file</strong>, or drop file here    
                                    </span>
                                </button> 

所以基本上我当前的节点是 button _ ngcontent 并且我想找到他的祖先或祖先的孩子中可用的第一个内容(以文本类型)。在这种情况下,我想要包含“Resume”作为内容的 h2 标签的文本。

'./ancestor::*[normalize-space(.//*)][1]'

此代码允许在祖先或祖先的任何后代中查找文本,但问题是,它也找到我的后代。因此,我从我不想要的标签中得到“选择文件”。

./ancestor::*[normalize-space(string(.)) and not(self::*) and not(.//*)][1]

应该是有效的,因为我排除了我的节点和我的节点后代,但在这种情况下我没有找到任何东西......

python html selenium-webdriver xpath
1个回答
0
投票

h2
不是祖先;也许可以尝试
preceding::
...

./preceding::*[text()[normalize-space()]][1]

我对 selenium 及其 xpath 支持不是很熟悉,而且你没有显示任何 python,所以我看不到你如何调用它,但上面是有效的 1.0 xpath。

© www.soinside.com 2019 - 2024. All rights reserved.