首页 > 解决方案 > 使用 normalize-space(text()) 找不到元素

问题描述

我在使用此 xpath //label[normalize-space(text())='F. nacimiento:'] HTML DOM查找以下元素时遇到问题 绿色元素是我想要使用前一个表达式定位的元素。我能够找到具有//label[normalize-space(text())='Servicio Decesos:']相同类型的任何其他元素的红色元素。

我尝试使用//label[text()=' F. nacimiento:']并且工作正常,但我使用循环和模式//label[normalize-space(text())=' + foobar + ']来识别元素的其余部分。

我的猜测是该文本字符串中的某些内容与空格或类似内容具有不同的字符代码,但我不知道如何检查。

有没有办法知道什么 normalize-space 返回,以便用它来标识元素?

我有一个不同的模式来识别元素,但我很固执,这是一个学习新东西的好机会,将来可能会派上用场来解决类似的问题。

我按要求下载了html代码:这是红色片段

<div class="sis-frame-bg">
        <table class="wideBox" align="center" cellpadding="2" cellspacing="0">
            <tbody>
                <tr>
                    <th width="20%" align="right">
                        <label for="DTRIES_CODZONA">
                            <span class="sis-important">*</span> Código postal:</label>
                    </th>
                    <td><input name="nombdato_CODZONA_1" type="text" id="DTRIES_CODZONA" size="8" maxlength="8" onblur="seccionDecesos();" value="" data-regexp="_codigoPostal" autocomplete="off"></td>
                </tr>
                <tr>
                    <th width="20%" align="right">
                        <label for="DTRIES_SERVICOD">
                            <span class="sis-important">*</span> Servicio decesos:</label>
                    </th>
                        <td>
                            <select name="nombdato_SERVICOD_1" id="DTRIES_SERVICOD" onchange="seccionDescricionServicio();" data-name="Servicio decesos" data-obligatorio="true" data-regexp="_cadena">
                                <option selected="selected" value="" title="Elegir"> Elegir</option> 
                            </select>
                        </td>
                </tr>
                <tr>
                    <th colspan="2" align="left">
                        <div id="datosServicioDecesos">
                            <!-- ASI261 -->
                            <!-- ASI261 -->
                        </div>
                    </th>
                </tr>
            </tbody>
        </table>
    </div>

还有绿色碎片

<table class="wideBox" cellspacing="0" cellpadding="2" align="center">
                    <tbody>
                        <tr>
                            <th align="right">
                                <label for="ASEG_FECHNACI">
                                    <span class="sis-important">*</span> F. nacimiento:</label>
                            </th>
                            <td>
                                <input id="ASEG_FECHNACI" name="fechnaci" type="text" maxlength="10" size="12" class="js-popUpCalendar js-dateformat hasDatepicker" onblur="formatoFecha(this);" data-name="FECHA DE NACIMIENTO ASEGURADO" data-obligatorio="true" title="" value="" data-regexp="_fecha" autocomplete="off">
                                <img class="ui-datepicker-trigger" src="./ico_calendar.png" alt="..." title="...">
                            </td>
                        </tr>   
                    </tbody>
                </table>

标签: xpathtextspacenormalizenormalize-space

解决方案


您可以使用以下 xpath。

//label[normalize-space(.)='* F. nacimiento:']

截屏

在此处输入图像描述


推荐阅读