如何在硒webdriver中使用xpath查找包含& nbsp;的文本的确切值? [英] How can I find the exact value using xpath in selenium webdriver for text that contains  ?

查看:231
本文介绍了如何在硒webdriver中使用xpath查找包含& nbsp;的文本的确切值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用xpath从代码中选择确切的文本"Section"时遇到问题.

I'm having an issue selecting the exact text 'Section' from the code using xpath.

**为了清楚起见,我要求从元素的innerText或innerHTML(而不是id)中选择确切的文本. **

** To be clear I require the exact text selection to be made from the innerText or innerHTML of the element if that is possible, not the id. **

我能够使用contains文本函数,但是也会导致其他包含"Section"的部分匹配也被返回/突出显示:

I'm able to use the contains text function, but that results in other partial matches that contain 'Section' being returned/highlighted as well:


//div[@aria-hidden='false']//ul/li[contains(text(),'Section')]

我尝试使用以下方法,但是我不知道语法是否正确,因为没有返回/突出显示了

I've tried using the following methods, but I don't know if I've got the syntax correct, as nothing is returned/highlighted:


//div[@aria-hidden='false']//ul/li[text()='Section')]

//div[@aria-hidden='false']//ul/li[.='Section']

//div[@aria-hidden='false']//ul/li[normalize-space(.)='Section']

这是检查节"节点时显示的内容:

This is what is shown when inspecting the Section node:


<li id="GOS--/40" class="nodecollapsed item parent-node xh-highlight" style="" xpath="1">
                                Section&nbsp;<span class="child-count"></span>
                            </li>

这是元素属性中显示的内容:

This is what is shown in the element properties:


id: "GOS--/40"
innerHTML: "↵                                Section&nbsp;<span class="child-count"></span>↵                            "
innerText: " Section "

以下是显示返回的其他部分匹配项的xml:

Here is the xml which shows the other partial matches that are returned:

<div class="selection-list-dialog modal-dialog Dialog">
    <div class="modal-content">
        <div class="modal-header SectionHeader">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <span class="modal-title" data-lang="StandardItems">Standard Items</span>
        </div>
        <div class="modal-body selection-list-container" style="margin-top: 30px" id="base">
            <div>
                <span data-lang="SelectItemInstructions">Select the items you are interested in from the list.</span>
            </div>
            <br/>
            <div class="pull-left selection-tree-container">
                <h4 class="selection-list-title">
                    <span data-lang="Available">Available</span>                    
                </h4>
                <ul class="selection-list selection-tree-list">



                            <li id="CS--/14" class="nodecollapsed item parent-node">
                                Country Section&nbsp;<span class="child-count"></span>
                            </li>                        


                            <li id="Sec1--/23" class="nodecollapsed item parent-node">
                                Section 1&nbsp;<span class="child-count"></span>
                            </li>


                            <li id="Sec2--/24" class="nodecollapsed item parent-node">
                                Section 2&nbsp;<span class="child-count"></span>
                            </li>


                            <li id="GOS--/40" class="nodecollapsed item parent-node">
                                Section&nbsp;<span class="child-count"></span>
                            </li>


                            <li id="RS--/43" class="nodecollapsed item parent-node">
                                Regional Section&nbsp;<span class="child-count"></span>
                            </li>

推荐答案

这是一项艰巨的任务.问题是您有许多类似的选项,都包含某种形式的"Section",很难将它们区分开.除此之外,每个元素都包含一个不间断的空格&nbsp;,这意味着normalize-space()也不(直接)起作用.

This was a tough one. The problem is that you have a number of similar options all containing "Section" in some flavor and it's hard to distinguish them apart. What adds to this is that each one contains a non-breaking space &nbsp; which means that normalize-space() won't work (directly) either.

但是...我发现下面的XPath可以工作.

But... I found that the below XPath will work.

//li[normalize-space()='Section\u00a0']

normalize-space() 删除空格(但不是&nbsp),因此您必须使用\u00a0在其中添加它.我已经在本地测试过,并且可以正常工作.

normalize-space() removes whitespace (but not &nbsp) so you have to add it in there with \u00a0. I've tested this locally and it's working.

这篇关于如何在硒webdriver中使用xpath查找包含&amp; nbsp;的文本的确切值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆