使用 Python 转义 XPath 文字 [英] Escaping XPath literal with Python

查看:25
本文介绍了使用 Python 转义 XPath 文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通用库,以使用 Selenium 2.0 Python 的 webdriver 设置自动化测试套件.

I'm writing a common library to setup an automation test suite with Selenium 2.0 Python's webdriver.

def verify_error_message_present(self, message):
    try:
        self.driver.find_element_by_xpath("//span[@class='error'][contains(.,'%s')]" % message)
        self.assertTrue(True, "Found an error message containing %s" % message
    except Exception, e:
        self.logger.exception(e)

我想在将消息传递给 XPath 查询之前对其进行转义,因此它可以支持消息"是否类似于使用的内存插槽数量(32)超过可用内存插槽数量(16)""

I would like to escape the message before passing it to XPath query, so it can support if 'message' is something like "The number of memory slots used (32) exceeds the number of memory slots that are available (16)"

如果不转义,xpath 查询将不起作用,因为它包含 '(' 和 ')'

Without escaping, the xpath query won't work since it contains '(' and ')'

我们可以使用哪个库在 Python 中执行此操作?

Which library can we use to do this in Python?

我知道这是一个简单的问题,但我在 Python 方面没有太多经验(刚刚开始).

I know that this is a simple question, but I don't have so much experience in Python (just started).

提前致谢.

其他信息:

在 firebug 中测试时,下面的查询不会返回任何结果:

During testing in firebug, the query below will return no result:

//span[@class='error'][contains(.,'The number of memory slots used (32) exceeds the number of memory slots that are available (16)')]

虽然下面的查询将返回所需的组件:

While the query below will return the desired component:

//span[@class='error'][contains(.,'The number of memory slots used \(32\) exceeds the number of memory slots that are available \(16\)')]

逻辑上这个问题可以通过用 \) 替换 ) 来解决这个特定的字符串文字,但是仍然有其他字符需要转义.那么是否有任何图书馆可以以适当的方式做到这一点?

Logically this problem can be solved by replacing ) with \) for this particular string literal, but then there are still the other characters need to be escaped. So is there any library to do this in a proper way?

推荐答案

括号应该没问题.它们位于由撇号分隔的 XPath 字符串文字内,因此它们不会过早结束 contains 条件.

Parentheses should be fine there. They're inside an XPath string literal delimited by apostrophe, so they do not prematurely end the contains condition.

问题是当你的字符串中有撇号时会发生什么,因为它们确实结束了字符串文字,破坏了表达式.不幸的是,XPath 字符串文字没有字符串转义方案,因此您必须使用表达式来解决它来生成麻烦的字符,通常采用 concat('str1', "'", 'str2') 形式concat('str1', "'", 'str2').

The problem is what happens when you have apostrophes in your string, since those do end the string literal, breaking the expression. Unfortunately there is no string escaping scheme for XPath string literals, so you have to work around it using expressions to generate the troublesome characters, typically in the form concat('str1', "'", 'str2').

这是一个 Python 函数:

Here's a Python function to do that:

def toXPathStringLiteral(s):
    if "'" not in s: return "'%s'" % s
    if '"' not in s: return '"%s"' % s
    return "concat('%s')" % s.replace("'", "',\"'\",'")

"//span[@class='error'][contains(.,%s)]" % toXPathStringLiteral(message)

这篇关于使用 Python 转义 XPath 文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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