Saltar al contenido

Usando XPATH para buscar texto que contenga

Esta sección fue analizado por especialistas para que tengas la garantía de la exactitud de nuestro tutorial.

Solución:

Parece que OpenQA, los chicos detrás de Selenium, ya han abordado este problema. Definieron algunas variables para hacer coincidir explícitamente los espacios en blanco. En mi caso, necesito usar un XPATH similar a //td[text()="$nbsp"].

Reproduje aquí el texto de OpenQA sobre este tema (que se encuentra aquí):

HTML normaliza automáticamente los espacios en blanco dentro de los elementos, ignorando los espacios iniciales / finales y convirtiendo espacios adicionales, pestañas y nuevas líneas en un solo espacio. Cuando Selenium lee texto fuera de la página, intenta duplicar este comportamiento, por lo que puede ignorar todas las pestañas y nuevas líneas en su HTML y hacer afirmaciones basadas en cómo se ve el texto en el navegador cuando se procesa. Hacemos esto reemplazando todos los espacios en blanco no visibles (incluido el espacio que no se rompe “ “) con un solo espacio. Todas las nuevas líneas visibles (
,

, y

 formatted
  new lines) should be preserved.

We use the same normalization logic on the text of HTML Selenese test case tables. This has a number of advantages. First, you don't need to look at the HTML source of the page to figure out what your assertions should be; " " symbols are invisible to the end user, and so you shouldn't have to worry about them when writing Selenese tests. (You don't need to put " " markers in your test case to assertText on a field that contains " ".) You may also put extra newlines and spaces in your Selenese tags; since we use the same normalization logic on the test case as we do on the text, we can ensure that assertions and the extracted text will match exactly.

This creates a bit of a problem on those rare occasions when you really want/need to insert extra whitespace in your test case. For example, you may need to type text in a field like this: "foo ". But if you simply write foo in your Selenese test case, we'll replace your extra spaces with just one space.

This problem has a simple workaround. We've defined a variable in Selenese, $space, whose value is a single space. You can use $space to insert a space that won't be automatically trimmed, like this: foo$space$space$space. We've also included a variable $nbsp, that you can use to insert a non-breaking space.

Note that XPaths do not normalize whitespace the way we do. If you need to write an XPath like //div[text()="hello world"] but the HTML of the link is really "hello world", you'll need to insert a real " " into your Selenese test case to get it to match, like this: //div[text()="hello$nbspworld"].


I found I can make the match when I input a hard-coded non-breaking space (U+00A0) by typing Alt+0160 on Windows between the two quotes...

//table[@id='TableID']//td[text()=' ']

funcionó para mí con el char especial.

Por lo que entendí, el estándar XPath 1.0 no maneja el escape de caracteres Unicode. Parece que hay funciones para eso en XPath 2.0, pero parece que Firefox no lo admite (o no entendí algo). Entonces tienes que ver con la página de códigos local. Feo, lo sé.

En realidad, parece que el estándar se basa en el lenguaje de programación que usa XPath para proporcionar la secuencia de escape Unicode correcta ... Entonces, de alguna manera, hice lo correcto.

Intenta usar la entidad decimal   en lugar de la entidad nombrada. Si eso no funciona, debería poder usar simplemente el carácter Unicode para un espacio que no se rompa en lugar del   entidad.

(Nota: no probé esto en XPather, pero lo probé en Oxygen).

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *