【问题标题】:Can't find element using normalize-space(text())使用 normalize-space(text()) 找不到元素
【发布时间】:2020-03-30 17:07:39
【问题描述】:

我在使用此 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>

【问题讨论】:

标签: xpath text space normalize normalize-space


【解决方案1】:

你可以使用下面的xpath。

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

截图

【讨论】:

  • 我可以使用所有标签元素,谢谢!你知道为什么我的 xpath 不工作吗? normalize-space(.) 是如何工作的?为什么我需要在 span 中添加 *?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2017-05-21
  • 2016-06-11
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2010-12-22
相关资源
最近更新 更多