由于机器人框架中的字符串中的撇号导致Xpath表达式错误 [英] Xpath expression error due to apostrophe in a String in robot framework

查看:68
本文介绍了由于机器人框架中的字符串中的撇号导致Xpath表达式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在机器人框架脚本中遇到以下错误.

I encountered the below error in my robot framework script.

InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //ul[@id='xyz-ul']/li[contains(text(),'Épargne en vue d\'un objectif précis')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//ul[@id='xyzul']/li[contains(text(),'Épargne en vue d\'un objectif précis')]' is not a valid XPath expression.

我正在尝试从Excel中获取法语数据代码",并传入xpath来标识并单击该元素.

I am trying to fetch the French data "Épargne en vue d'un objectif précis" from an excel and passing in an xpath to identify and click the element.

该元素是一个下拉列表.这是方案之一.我还有许多其他失败的情况.有人可以帮忙解决这个问题吗?

The element is a dropdown. This is one of the scenarios. I have many other such scenarios which are failing. Could someone help how to handle this?

推荐答案

XPath没有转义序列.

XPath has no escape sequences.

//ul[@id='xyz-ul']/li[contains(text(),'Épargne en vue d\'un objectif précis')]

//ul[@id='xyz-ul']/li[contains(text(),'Épargne en vue d'un objectif précis')]

会的.

要视情况而定,是否可以减轻此错误,或者您不走运.

It depends on the circumstances if you can mitigate this error or if you are out of luck.

在测试框架的背景下,您可能很不走运,但是您需要显示更多代码才能获得更好的答案.

In the context of a testing framework you might very well be out of luck, but you need to show more of your code to get a better answer.

有两种方法可以在XPath中将单引号引起来.

There are two ways to get single quotes into a string in XPath.

  1. 对字符串使用双引号.然后,您可以在内部使用单引号(反之亦然).

  1. Use double quotes for the string. Then you can use single quotes inside (and vice versa).

//ul[@id='xyz-ul']/li[contains(text(),"Épargne en vue d'un objectif précis")]

  • 在这种情况下(如您所处的情况)不可行时,因为您不知道用户提供的值是包含单引号还是双引号,因此必须使用 concat() XPath函数像这样:

  • When this is not feasible, like in your situation, because you never know whether a user-supplied value contains single- or double quotes, you must use the concat() XPath function like this:

    //ul[@id='xyz-ul']/li[contains(text(), concat('Épargne en vue d', "'", 'un objectif précis'))]
    

  • 第二个版本看起来很困难,但实际上可以通过拆分和连接输入字符串来可靠地构建它,就像这样

    The second version looks difficult, but it can in fact be built reliably by splitting and joining the input string, like this

    1. 输入的字符串为 preparcis对象代码
    2. '处拆分会产生Épargneen vue d un objectifprerécis
    3. ','",'一起加入会给出Épargneen vue d','",'un objectifprécis
    4. concat('')包装后得到 concat('Épargneen vue d',','un objectifprécis')
    1. Input string is Épargne en vue d'un objectif précis
    2. Splitting at ' gives Épargne en vue d and un objectif précis
    3. Joining with ', "'", ' gives Épargne en vue d', "'", 'un objectif précis
    4. Wrapping with concat(' and ') gives concat('Épargne en vue d', "'", 'un objectif précis'),

    恰好是您可以使用占位符将其放入XPath表达式中的字符串,如下所示:

    which is exactly the string you can put into an XPath expression with a placeholder, like this one:

    ${dropdown_selection_first_part_xpath} =  //ul[@id='xyz-ul']/li[contains(text(), 
    ${dropdown_selection_second_part_xpath} =  )]
    ${dropdown_xpath} =  //div[@id='xyz-widget']/div/div[2]/div[1]
    
    ${escaped_input_purpose_of_account} = (somehow to the 4 steps above)
    
    ${dropdown_selection_xpath} =  catenate  SEPARATOR=            ${dropdown_selection_first_part_xpath}    ${escaped_input_purpose_of_account}    ${dropdown_selection_second_part_xpath}
    

    这种方法适用于所有输入(空字符串,不带引号的字符串,具有单引号和双引号的任意混合的字符串),并且始终创建符合您期望的语法有效的XPath.

    This approach works for all inputs (empty string, strings with no quotes, strings with any mix of single- and double quotes) and always creates syntactically valid XPath that does what you expect.

    这篇关于由于机器人框架中的字符串中的撇号导致Xpath表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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